Welcome, guest | Sign In | My Account | Store | Cart

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:

Python, 57 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class SuperWrapper(object):
	"""Make sure descriptor super(cls) is invoked
        even when called on a class."""
	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 SuperMeta(type):
	"Automatically add attribute __sup for every class."
	def __init__(cls, name, bases, clsdict):
		setattr(cls, '_{}__sup'.format(name), SuperWrapper(cls))
		super(SuperMeta, cls).__init__(name, bases, clsdict)

# Usage examples:

class A(object):
	__metaclass__ = SuperMeta
	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()
Created by sunqingyao on Sun, 31 Jul 2016 (MIT)
Python recipes (4591)
sunqingyao's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks