Nov-19-2022, 05:34 AM
(This post was last modified: Nov-19-2022, 12:46 PM by deanhystad.)
From your description there is no subclass relationship between A, B and C. An instance of class A has an instance variable that is an instance of B, but that only means that class A has to know about class B. This is really no different than a class knowing about str or int.
As for your two patterns, why choose? If I understand correctly, you can do both.
The object referenced by variable "c" is the same object that is referenced by b.c which is the same object referenced by a.b.c. Assigning "b.c = c" does not create a new object. It creates an attribute in "b" named "c" (or globally as "b.c") that reference the same object referenced by the variable "c". Only 1 ojbect. Multiple references.
The only way to have a "temporary object" is to eliminate all references to the object.
As for your two patterns, why choose? If I understand correctly, you can do both.
from dataclasses import dataclass # Using data classes because they make this easy. Can do same thing with regular classes @dataclass class C(): value: int @dataclass class B(): value: int c: C = C(0) @dataclass class A(): value: int b: B = B(0) a1 = A(1) # This uses pattern 1, A makes a default B which makes a default C a1b2 = A(1, B(2)) a1b2c3 = A(1, B(2, C(3))) # This uses pattern 2. B and C are made external to A. print("a1", a1) print("a1b2", a1b2) print("a1b2c3", a1b2c3) print(a1b2c3.b.c.value)
Output:a1 A(value=1, b=B(value=0, c=C(value=0)))
a1b2 A(value=1, b=B(value=2, c=C(value=0)))
a1b2c3 A(value=1, b=B(value=2, c=C(value=3)))
3
I have a few comments about your postQuote:Having a global builder-function which starts creating a temporary instance of Class C, setting all its attributes.You may be confused about variables and objects. Variables are not objects. Variables are convenient names that you use to reference objects. You would not create a "temporary instance of Class C". If I do this:
When all is done, adding this instance as a child to a new instance of Class B, then setting all attributes of B.
When all is done, adding this instance as a child to a new instance of Class A, then setting all attributes of A, which is the highest class. Then its done.
c = C(1) b = B(2, c) a = A(3, b)"a", "b" and "c" are just names. "c" is not an instance of class C, it is only a variable that references an instance of class C. Later in my program I might reuse variable "c" to reference the sum of "5 + 1".
The object referenced by variable "c" is the same object that is referenced by b.c which is the same object referenced by a.b.c. Assigning "b.c = c" does not create a new object. It creates an attribute in "b" named "c" (or globally as "b.c") that reference the same object referenced by the variable "c". Only 1 ojbect. Multiple references.
The only way to have a "temporary object" is to eliminate all references to the object.
c = C(1) c = None # Eliminates only reference to C instance. C instances is garbage collected