Reputation: 11543
I really need to find some way to superclass's classmethod from subclasses of that superclass.
here is the generalized code:
class A(object):
def __init__(self):
print "A init"
@classmethod
def _method(cls):
print cls
return cls()
class B(A):
def __init__(self):
print "B init"
class C(B):
def __init__(self):
print "C init"
@classmethod
def _method(cls):
print "calling super(C)'s classmethod"
return super(C)._method()
c = C._method()
which results in :
Traceback (most recent call last):
File "C:/Python27x64/testclass", line 26, in <module>
c = C._method()
File "C:/Python27x64/testclass", line 22, in _method
return super(C)._method()
AttributeError: 'super' object has no attribute '_method'
note that from c = C._method()
, I am calling uninitialized class C
's classmethod. and from C
, I call also uninitialized class A
or B
(traversing through the MRO)'s classmethod.
How can I achieve this?
Upvotes: 2
Views: 278
Reputation: 4673
You need to include the cls
variable in the super
call:
class C(B):
def __init__(self):
print "C init"
@classmethod
def _method(cls):
print "calling super(C)'s classmethod"
return super(C, cls)._method()
Upvotes: 1