Home > Mobile >  How to call child class variable into parent class variable
How to call child class variable into parent class variable

Time:12-08

I want to call child class function inside parent class so that I can take values of child inside parent class and use it.

I want to print the child age inside the parent class. How can I call that? I have tried the others' solution but here there are some dependencies I have where I have to create parent object and call all its functions and same for child. I am learning inheritance right now and now it's confusing me.

class Parent():
    
    def __init__(self):
       self.name = name
          
    def parent_age(self):
     age_p = "44"
     print("the child f{age_p}")

class Child(Parent):
    
    def __init__(self, parent_name):
        self.name = parent_name.name
        
    def child_age(self):
        age = "13"
        print("the child f{age}")
        
        
    p = Parent()
    p.parent_age()
    c = Child(p)
    c.child_age()

CodePudding user response:

You can't reach a variable of the derived class in the base class. The way you wrote your classes allows creation of Parent object without Child. Plus your classes don't make sense because they are basically saying a child is a type of a parent.

Instead you should have a child class and parent class that contains child objects in something like a list. Then you could reach all of the child properties in the parent class.

  • You also have an error in the init function of the parent class - you don't pass name as an argument

Here is an example of what you should do:

class Child():
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def __str__(self):
        return f'Child name: {self.name}, age: {self.age}'

class Parent():
    
    def __init__(self, name, age, children):
       self.name = name
       self.age = age
       self.children = children
          
    def __str__(self):
        s = f'Parent name: {self.name}, age: {self.age}'
        for child in self.children:
            s = s   '\n* '   str(child)
        return s

        
p = Parent('Ted', 43, (Child('Ann', 12), Child('Chris', 6)))
print(p)

Output:

Parent name: Ted, age: 43
* Child name: Ann, age: 12
* Child name: Chris, age: 6

If you noticed, there is repetition on some of the parent and child properties. That because they have something in common - they are both people. All have some general properties in common such as all have age and all have a name.

If you wish, you can create a third class - person - which both parent and child will inherit from it.

CodePudding user response:

There are some weird workarounds to this

class Parent():

    def __init__(self, name):
        self.name = name

    def parent_age(self, child):
        age_p = "44"
        print("the child is", child.age)


class Child(Parent):

    def __init__(self, parent):
        self.name = parent.name
        self.age = None

    def child_age(self, age):
        self.age = age

# Create parent and child class
p = Parent("Father")
c = Child(p)

# Call the child class' child_age method to set the age
c.child_age(12)

# Call the parent class' parent_age method
p.parent_age(c)

This code returns

"the child is 12"

The problem with this way of doing things though is that it doesn't actually matter if the child class is inherited from the parent class which I suppose defeats the purpose

  • Related