I get some error that I can't figure out. Any clue what is wrong with my sample code?
class B:
def meth(self, arg):
print arg
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
I got the sample test code from help of 'super' built-in method.
Here is the error:
Traceback (most recent call last):
File "./test.py", line 10, in ?
print C().meth(1)
File "./test.py", line 8, in meth
super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj
FYI, here is the help(super) from python itself:
Help on class super in module __builtin__:
class super(object)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
| Typical use to call a cooperative superclass method:
| class C(B):
| def meth(self, arg):
| super(C, self).meth(arg)
|
This question is related to
python
object
inheritance
parent
super
I was also faced by the posted issue when I used python 2.7. It is working very fine with python 3.4
To make it work in python 2.7 I have added the __metaclass__ = type
attribute at the top of my program and it worked.
__metaclass__
: It eases the transition from old-style classes and new-style classes.
If the python version is 3.X, it's okay.
I think your python version is 2.X, the super would work when adding this code
__metaclass__ = type
so the code is
__metaclass__ = type
class B:
def meth(self, arg):
print arg
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
Also, if you can't change class B, you can fix the error by using multiple inheritance.
class B:
def meth(self, arg):
print arg
class C(B, object):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
Source: Stackoverflow.com