Depending on the name of the first argument, self.__sup
or cls.__sup
behaves like super()
in Python 3, while this code is written in Python 2.7. It works for both ordinary methods and class methods(static methods don't use super()
). See my code for detailed examples:
class mySuper(object):
def __init__(self, cls):
self.cls = cls
def __get__(self, obj, cls):
if obj is None:
return super(self.cls, cls)
else:
return super(self.cls, obj)
class Meta(type):
def __init__(cls, name, bases, clsdict):
setattr(cls, '_' + name + '__sup', mySuper(cls))
super(Meta, cls).__init__(name, bases, clsdict)
# Usage examples:
class A(object):
__metaclass__ = Meta
def meth(self):
print 'A.meth(%s)' % self
@classmethod
def clsmeth(cls):
print 'A.clsmeth(%s)' % cls
class B(A):
def meth(self):
self.__sup.meth() # super().meth()
print 'B.meth(%s)' % self
@classmethod
def clsmeth(cls):
cls.__sup.clsmeth() # super().clsmeth()
print 'B.clsmeth(%s)' % cls
class C(A):
def meth(self):
self.__sup.meth()
print 'C.meth(%s)' % self
@classmethod
def clsmeth(cls):
cls.__sup.clsmeth()
print 'C.clsmeth(%s)' % cls
class D(B, C):
def meth(self):
self.__sup.meth()
print 'D.meth(%s)' % self
@classmethod
def clsmeth(cls):
cls.__sup.clsmeth()
print 'D.clsmeth(%s)' % cls
d = D()
d.meth()
d.clsmeth()
Here is the output:
A.meth(<__main__.D object at 0x103c8aed0>)
C.meth(<__main__.D object at 0x103c8aed0>)
B.meth(<__main__.D object at 0x103c8aed0>)
D.meth(<__main__.D object at 0x103c8aed0>)
A.clsmeth(<class '__main__.D'>)
C.clsmeth(<class '__main__.D'>)
B.clsmeth(<class '__main__.D'>)
D.clsmeth(<class '__main__.D'>)
I'm sorry if the examples are not very meaningful, but I'm just a beginner. (Maybe emulating super()
itself is pointless.)
Apart from the "Usage examples", it's just a little snippet, so please tell me any problem you can spot in this piece of code.