Super() function

Delaney, Timothy (Tim) tdelaney at avaya.com
Mon Mar 29 02:58:07 CEST 2010


> Gabriel Genellina wrote:
>> Alan Harris-Reid <aharrisreid at googlemail.com> escribió:
>>
>>> Using Python 3.1, I sometimes use the super() function to call the 
>>> equivalent method from a parent class, for example
>>>
>>> def mymethod(self):
>>>     super().mymethod()
>>>     some more code...
>>>
>>> Is there any way of writing the code so that the super() call is 
>>> generic and automatically recognises the name of the current method 
>>> (ie. something like super().thismethod()) or do I always have to 
>>> repeat the method name after super()?
>>
>> This recipe does what you want:
>> http://code.activestate.com/recipes/286195-selfsuper/
>> (but requires a bit of black magic...)
>>
> Hi Gabriel - thanks for the reply.
>
> Goodness me - that's a mighty complicated recipe for what I want to achieve!
> I think I'll stick with repeating the method name - it's a small price to pay.

I wrote that recipe back before Python 3.x supported super() without parameters,
and near equivalent functionality was part of the new super PEP as an option.
However, Guido decided to go with the much simpler super().attr with no shortcutting.

If you think the pure-python version of the recipe is complicated, you should have
a look at the performance-optimised Pyrex version ;)

Tim Delaney



More information about the Python-list mailing list