Python Forum
which design / pattern when building classes and subclasses
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which design / pattern when building classes and subclasses
#1
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?
Reply
#2
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
Reply
#3
(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?
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 3,189 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  Classes, OOP, building deck of 52 playing cards Drone4four 9 3,937 Jan-19-2022, 02:53 PM
Last Post: Drone4four
  User Subclasses holyghost 6 3,214 Mar-17-2021, 12:33 PM
Last Post: buran
  Design Pattern for accessing subclass attributes UGuntupalli 2 2,108 Jul-30-2019, 11:09 PM
Last Post: UGuntupalli
  Factory Design Pattern Prince 1 2,496 Apr-06-2018, 10:00 AM
Last Post: Larz60+
  Using classes? Can I just use classes to structure code? muteboy 5 5,085 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020