Home > OS >  Is it able to create a sub object from a parent object in Python
Is it able to create a sub object from a parent object in Python

Time:02-07

I want to create an object with another class object as parent. For example:

class Parent:
    value = 0
    def __init__(self):
        self.value = int(input())
        self.call_subclass()

    def call_subclass(self):
        self.sub = Child()

class Child(Parent):
    def __init__(self):
        print(self.value)

obj = Parent()

Let's say the input is 3, my expected output of print is also 3. But the actual output of above code is 0. The point is that, I want the sub class to inherit the self object of Parent. Not only inheriting the method and class variable from the Parent class, but also those instance variables of an actual called and existed Parent class object.

I know that there is a way to do this, is like below:

class Parent:
    value = 0
    def __init__(self):
        self.value = int(input())
        self.call_subclass()

    def call_subclass(self):
        self.sub = Child(self.value)

class Child(Parent):
    def __init__(self, value):
        self.value = value
        print(self.value)

obj = Parent()

But this will be so clumsy if there is many instance variable. Also, the value is passing to Child by value, but not reference. So Child cannot get the most update instance variable value from Parent, but is what I want it to be able to implement.

Also, I know there is another way as below:

class Parent:
    value = 0
    def __init__(self):
        self.value = int(input())
        self.call_subclass()

    def call_subclass(self):
        self.sub = Child(self)

class Child(Parent):
    def __init__(self, parent):
        self.parent = parent
        print(self.parent.value)

obj = Parent()

But this way seems not so concise. Also, some redundant resource will be produced, for example the Parent class' variable value = 0 will be inherited but with no use(the Child class should only interest in the instance variable and method of Parent class object obj). I would like to know if there is better method to achieve my purpose.

I understand there should be somebody already ask similar question, but I don't know what's the name of this technic. Therefore, I cannot find related information and decide to ask.

CodePudding user response:

From your comment, I'd say you need something like this:

class Parent:
    def __init__(self):
        self.value = int(input())

class Child:
    def __init__(self, parent):
        self._parent = parent

    def print_value(self)
        print(self._parent.value)

parent = Parent()  # input a number, for example 17

child1 = Child(parent)
child1.print_value()  # print 17
child2 = Child(parent)
child2.print_value()  # print 17

parent.value = 19
child1.print_value()  # print 19
child2.print_value()  # print 19

It is unclear what call_subclass would do. If you want to call something from all children of a parent, then

class Parent:
    def __init__(self):
        self.value = int(input())
        self._children = list()

    def register_child(self, child):
        self._children.append(child)

    def call_children_f(self, foo):
        for child in self._children:
            child.f(foo)

class Child:
    def __init__(self, parent, x):
        self._parent = parent
        self.x = x
        self._parent.register_child(self)

    def print_value(self)
        print(self._parent.value)

    def f(self, foo):
        print(f"Parent: {self._parent.value}, foo: {foo}, x: {self.x}")

parent = Parent()  # input a number, for example 17

child1 = Child(parent, 19)
child2 = Child(parent, 23)

parent.call_children_f(29)

This will print:

Parent: 17, foo: 29, x: 19
Parent: 17, foo: 29, x: 23

CodePudding user response:

if I am understanding correctly, I think you should just instance a Child class and not a Parent class.

Change the last line (from the original code) to this:

obj = Child()
  •  Tags:  
  • Related