Recently, I encountered a problem with metaclass calling a derived classmethod.
For example, I get a simple baseclass testA
, which has an classmethod do1(a)
class testA(object):
@classmethod
def do1(cls, a):
print "in testA:",cls, a
Then I build a metaclass which actually do nothing but print the cls:
class testMetaA(type):
def __init__(cls,cname,bases,cdict):
print "in testMetaA: %s"%cls
Then I could use the metaclass to build a subclass testB
, which works as expected:
class testB(testA):
@classmethod
def do1(cls, a):
print "in testB: %s"%cls
super(testB, cls).do1(a)
__metaclass__=testMetaA
It will print: in testMetaA: <class '__main__.testB'>
; and the testB.do1(a)
works as expected:
>>> testB.do1('hello')
in testB: <class '__main__.testB'>
in testA: <class '__main__.testB'> hello
However, if I try to call the classmethod inside the metaclass which contains a "super
" as following testMetaB
, it will raise an error: NameError: global name 'testC' is not defined
.
class testMetaB(type):
def __init__(cls,cname,bases,cdict):
print "in testMetaB: %s"%cls
cls.do1("hello")
class testC(testA):
@classmethod
def do1(cls, a):
print "in testC: %s"%cls
super(testC, cls).do1(a)
__metaclass__=testMetaB
I finally find a way to solve it by use super(cls, cls)
instead of super(testC, cls)
:
class testD(testA):
@classmethod
def do1(cls, a):
print "in testD: %s"%cls
super(cls, cls).do1(a)
__metaclass__=testMetaB
It will print as:
in testMetaB: <class '__main__.testD'>
in testD: <class '__main__.testD'>
in testA: <class '__main__.testD'> hello
The testD.do1(a)
also works as expected:
>>> testD.do1('Well done')
in testD: <class '__main__.testD'>
in testA: <class '__main__.testD'> Well done
Now I am wondering which is the most correct way to use super in a classmethod? Should one always use super(cls,cls)
instead of explicitly writing a current class name?
Thanks!
@jsbueno
If some piece of code resorts to tricks like dynamically creating derived classes, that is important - one should not use the class name as first parametere to Super if that name is assigned to another object than the class itself. Instead, cls for class methods, or
self.__class__
for instance methods can be passed to Super.
Does this means it is a bad idea to use class name to super in general?
To myself, I usually use super(type(self),self)
instead of super(type(self.__class__),self)
for normal method. I do not know if there is any major advantage to use self.__class__
.
I repeat @jsbueno example like this, here the C use super(type(self),self)
. So D2()
will not change the behavior while the class C
gets changed.
>>> class A(object):
def do(self):
print "in class A"
>>> class B(A):
def do(self):
super(B, self).do()
>>> class C(A):
def do(self):
super(type(self),self).do()
>>> D1=B
>>> D2=C
>>> D1().do()
in class A
>>> D2().do()
in class A
>>> class B(A):
def do(self):
print "in new class B"
>>> D1().do()
Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
D1().do()
File "<pyshell#37>", line 3, in do
super(B, self).do()
TypeError: super(type, obj): obj must be an instance or subtype of type
>>> class C(A):
def do(self):
print "in new class C"
>>> D2().do()
in class A
according to @Don Question's suggestion, I put the python version here: sys.version= 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1]
However, if I try to call the classmethod inside the metaclass which contains a "super" as following testMetaB, it will raise an error: NameError: global name 'testC' is not defined.
The name TestC
will only be bound to the new class after the MetaClass finished it's work - and that is after returning from it's __init__
(and before __init__
, the __new__
) method.
When we use the "super" call usign the class name as the first parameter, the class name does not appear there magically: it is a (module) global variable, to which the class itself is assigned - in normal circunstances.
In this case, the name has not been assigned yet - however, as it is a classmethod, yuu have a reference to the class in the cls
variable- that is why it works.
If some piece of code resorts to tricks like dynamically creating derived classes, that is important - one should not use the class name as first parametere to Super if that name is assigned to another object than the class itself. Instead, cls
for class methods, or self.__class__
for instance methods can be passed to Super.
Here is a snippet showing the global name binding for the class name is what super takes:
>>> class A(object):
... def do(self):
... print "In class A"
...
>>> class B(A):
... def do(self):
... super(B, self).do()
...
>>> C = B
>>> C().do()
In class A
>>> class B(object):
... def do(self):
... print "in new class B"
...
>>> C().do()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in do
TypeError: super(type, obj): obj must be an instance or subtype of type
>>>