I am trying to overwrite a classmethod as seen in the code below but am getting the following error
TypeError: must be type, not classobj
Why is this happening, and how can I fix it?
class A(object):
@classmethod
def test(cls, data):
pass
class B(A):
@classmethod
def test(cls, data):
pass
class C(B):
@classmethod
def test(cls, data):
# do something to data
return super(B, cls).test(data)
super is an object, not a class. Since test is explicitly a class method, you need to call it using the class of super (in this case "A").
Try replacing the last line with return A.test(data)
super
only works with new-style classes (which in the 2.x series means inheriting from object
), but A
is an old-style class.
To fix it, replace class A():
with class A(object):
.
A
is an old-style class (it does not inherit from object
), but super
can only be used with new-style classes. Just fix A
:
class A(object):
@classmethod
def test(cls, data):
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.