0

what should be done in order to execute it

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

class child(parent):
    def __init__(self,name,age,gender):
        super(parent,self).__init__(self.name,self.age)
        print gender

c=child("xyz",22,"male")
c.printout()

I am a newbie in the world of python, unable to figure out what is the problem

3 Answers 3

2

super() only works for new-style classes; add object to the base classes for parent:

class parent(object):

You may want to adjust your super() call as well. You need to give the current class, not the parent class to start searching from, and self.name and self.age are still set to None by the time your __init__ is called, but you seem to want to pass on the name and age arguments:

def __init__(self, name, age, gender):
    super(child, self).__init__(name, age)
    print gender

With these changes, the code works:

>>> c = child("xyz", 22, "male")
male
>>> c.printout()
xyz
22
Sign up to request clarification or add additional context in comments.

Comments

1

super() works for new style classes only (In Python3, everything is new style). SO you need

class parent(object):

Also in call to super, the first argument is the name of the child class not the parent class. The call in child class should be

super(child, self).__init__(name, age)

Comments

1

You need to inherit from object to make super() work, also self.name and self.age are always None when you pass them to the super() call.

class parent(object):

And:

super(child, self).__init__(name, age)

2 Comments

Yes, they have been set; on the class. That's probably not what the OP wants, however. ;-)
Ah, up there on the class itself. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.