Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get variable from parent class for use in method of child class [duplicate]

I'm trying to understand how parent and child classes in Python work and I ran into this seemingly simple problem:

class parent(object):

    def __init__(self):
        self.data = 42


class child(parent):

    def __init__(self):
        self.string = 'is the answer!'

    def printDataAndString(self):
        print( str(self.data) + ' ' + self.string )


c = child()
c.printDataAndString()

I'm expecting the string 42 is the answer! but I get

AttributeError: 'child' object has no attribute 'data'

What am I missing?

I experimentated with pass and also super(parent,...) but couldn't get it right.

like image 835
Robert Seifert Avatar asked Sep 03 '15 09:09

Robert Seifert


People also ask

Can we use variable of parent class in child class?

The only unusual aspect is that, within child class method definitions, you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

How do you access the attributes of parent class in child class?

To access parent class attributes in a child class: Use the super() method to call the constructor of the parent in the child. The __init__() method will set the instance variables. Access any of the parent class's attributes or methods on the self object.

How do you access the variable of a parent class?

1) super is used to refer immediate parent class instance variable. We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

What happens if the parent and the child class have a field with same identifier?

What happens if the parent and the child class have a field with same identifier? Super class field member will be hidden at the sub class and super class field could be accessed using super keyword.


1 Answers

Since your child has its own __init__() function, you need to call the parent class' __init__() , otherwise it does not get called. Example -

def __init__(self):
    super(child,self).__init__()
    self.string = 'is the answer!'

super() from documentation -

super(type[, object-or-type])

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.

So the first argument to super() should be the child class (whose' parent class' method you want to call) , and the second argument should be the object itself , that is self. Hence , super(child, self) .

In Python 3.x , you can simply call -

super().__init__()

And it would call the __init__() method from the correct parent class.

like image 172
Anand S Kumar Avatar answered Oct 20 '22 00:10

Anand S Kumar