[Python-3000] auto-super()

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed Apr 19 00:39:56 CEST 2006


Ian Bicking wrote:

> But how can we do that without the function being bound to a class?
> self.whoami() in A.dostuff needs to access the 'real' self. 
> self.super in B.dostuff should be super(B, self), even when
> self.__class__ is C. 
> 
> Because Python *isn't* reading the functions statically to determine
> class and method layout, actually making this work seems very hard.

Believe me, it is. Have a look at my `self.super` recipe at:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195

and the considerably faster Pyrex version at:
http://members.optusnet.com.au/tcdelaney/python.html#autosuper

I'm sure there are some edge cases which are not covered by the
implementation, but it gets the vast majority of them. And I haven't
tested it with Jython or IronPython.

It works using sys._getframe() (to get the code object and method name),
then walks the MRO to find which class has a method with the same name
and code object (there's a bit more to it to prevent recursion).

`self.super` is also callable - if you call `self.super(params)` it
invokes the method on the superclass with the same name as the current
method.

Tim Delaney


More information about the Python-3000 mailing list