super and classmethod
Here's a nice example of why you have to use super in order for classmethod to work correctly in derived classes.
The code:
class Base(object):
@classmethod
def method(cls):
print 'In base: ', cls.__name__
print
class DerivedWithoutSuper(Base):
@classmethod
def method(cls):
print 'In derived:', cls.__name__
Base.method()
class DerivedWithSuper(Base):
@classmethod
def method(cls):
print 'In derived:', cls.__name__
super(DerivedWithSuper, cls).method()
if __name__ == '__main__':
DerivedWithoutSuper().method()
DerivedWithSuper().method()
The output:
$ python super_classmethod.py In derived: DerivedWithoutSuper In base: Base In derived: DerivedWithSuper In base: DerivedWithSuper
Tags: python
Tue, 18 Sep 2007 16:05 UTC