Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 31 lines (21 sloc) 1.15 kB
ff2ad0c @rfk initial commit
authored
1
748b117 @rfk Update docstring and readme.
authored
2 magicsuper: backport the magical zero-argument super() to python2
3 ==================================================================
ff2ad0c @rfk initial commit
authored
4
748b117 @rfk Update docstring and readme.
authored
5 This is an (awful, hacky, wtf-were-you-thinking) attempt to port the magical
6 zero-argument super() call from python3 to python2.
ff2ad0c @rfk initial commit
authored
7
748b117 @rfk Update docstring and readme.
authored
8 In standard python2 usage of the super() builtin, you have to repeat both the
9 class and instance objects when you call super, like this:
ff2ad0c @rfk initial commit
authored
10
11 class Hello(Base):
12 def hello(self):
13 super(Hello,self).hello()
14
748b117 @rfk Update docstring and readme.
authored
15 Using magicsuper, you can get the friendlier behaviour from python3 where it
ff2ad0c @rfk initial commit
authored
16 just figures out the correct call at runtime:
17
18 class Hello(Base):
19 def hello(self):
20 super().hello()
21
748b117 @rfk Update docstring and readme.
authored
22 Of course, you can still explicitly pass in the arguments if you want to do
23 something strange. Sometimes you really do want that, e.g. to skip over
ff2ad0c @rfk initial commit
authored
24 some classes in the method resolution order.
25
748b117 @rfk Update docstring and readme.
authored
26 How does it work? By inspecting the calling frame to determine the function
27 object being executed and the object on which it's being called, and then
28 walking the object's __mro__ chain to find out where that function was
29 defined. Yuck, but it seems to work...
ff2ad0c @rfk initial commit
authored
30
Something went wrong with that request. Please try again.