Python Forum
which design / pattern when building classes and subclasses - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: which design / pattern when building classes and subclasses (/thread-38742.html)



which design / pattern when building classes and subclasses - Phaze90 - Nov-18-2022

I need to build a mother-child-like class tree.

Dont know which is the best practice how to structure it.

I could define a top-level class A which has some own attributes and at least 1 sub class B.
The class B itself has again some own attributes and at least 1 sub class C, and so on...
I see 2 ways:
(1)
call instance of Class A, which has a method "fill_own_attributes" and "add subclass(B)".
When the instance of A is called it automatically sets its attributes and adds the subclass B.
When the instance of B is called it automatically sets its attributes and adds the subclass C.

(2)
Having a global builder-function which starts creating a temporary instance of Class C, setting all its attributes.
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.

I think first one looks better, but is there any other way? Someone has any info material about my problem?


RE: which design / pattern when building classes and subclasses - deanhystad - Nov-19-2022

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.
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 post
Quote:Having a global builder-function which starts creating a temporary instance of Class C, setting all its attributes.
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.
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:
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



RE: which design / pattern when building classes and subclasses - Gribouillis - Nov-19-2022

(Nov-18-2022, 08:53 PM)Phaze90 Wrote: I need to build a mother-child-like class tree.

Dont know which is the best practice how to structure it.

I could define a top-level class A which has some own attributes and at least 1 sub class B.
The class B itself has again some own attributes and at least 1 sub class C, and so on...
It looks like you are confusing inheritance with composition, could you give a concrete example of your design?