Skip to content Skip to sidebar Skip to footer

Typeerror In Python Single Inheritance With "super" Attribute

I never expected the inheritance of Python 2.7 is so anti-human. Why the following code give me a TypeError? >>> class P: ... def __init__(self, argp): ... sel

Solution 1:

There are two responses possible for your question, according to the version of Python you're using.

Python 2.x

super has to be called like this (example for the doc):

classC(B):defmethod(self, arg):
        super(C, self).method(arg)

In your code, that would be:

>>>classP(object):    # <- super works only with new-class style...def__init__(self, argp):...        self.pstr=argp...>>>classC(P):...def__init__(self, argp, argc):...super(C, self).__init__(argp)...        self.cstr=argc

You also don't have to pass self in argument in this case.

Python 3.x

In Python 3.x, the super method has been improved: you can use it without any parameters (both are optional now). This solution can work too, but with Python 3 and greater only:

>>>classC(P):...def__init__(self, argp, argc):...super().__init__(argp)...        self.cstr=argc

Post a Comment for "Typeerror In Python Single Inheritance With "super" Attribute"