I want to call parent class method using super() in Python 2.
In Python 3, I'd code it like this:
class base:
@classmethod
def func(cls):
print("in base: " + cls.__name__)
class child(base):
@classmethod
def func(cls):
super().func()
print("in child: " + cls.__name__)
child.func()
with this output:
in base: child
in child: child
However, I have no idea, how do this in Python 2. Of course, I can use base.func()
, but I don't like to specify parent class name in addition and mainly I get unwanted result:
in base: base
in child: child
With cls
(cls is child
) as first argument in super()
function call, I get this error:
TypeError: must be type, not classobj
Any idea how do it using super()
or analogous function in which I don't have to specify name of parent class?
furthering the other answer you can do classmethods for it like
class base(object):
@classmethod
def func(cls):
print("in base: " + cls.__name__)
class child(base):
@classmethod
def func(cls):
super(cls, cls).func()
print("in child: " + cls.__name__)
child.func()