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()?
TIA,
Alan
To the best of my knowledge, you always need to repeat the method
name. super() returns a proxy object that has no awareness of its
context, so it's unaware of the method it's being called within (in
fact, there's nothing that restricts super() to only being used in
methods...).
You could probably write a wrapper function that uses inspect to
determine in which context super() is being called, but unless you're
regularly finding this to be a problem with refactoring I wouldn't
worry about it.
> 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...)
--
Gabriel Genellina
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.
Regards,
Alan
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
Where can we look at it? The link in the activestate recipe does not work
anymore :(
--
Gabriel Genellina
> En Sun, 28 Mar 2010 21:58:07 -0300, Delaney, Timothy (Tim) <tdel...@avaya.com> escribió:
>>> Gabriel Genellina wrote:
>>>> Alan Harris-Reid <aharr...@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...
>>>>
>>>> This recipe does what you want:
>>>> http://code.activestate.com/recipes/286195-selfsuper/
>> If you think the pure-python version of the recipe is complicated, you
>> should have a look at the performance-optimised Pyrex version ;)
>
> Where can we look at it? The link in the activestate recipe does not work anymore :(
You're right - I've changed ISPs. I'll have to find somewhere else to upload it and change the link. Can't do it today unfortunately.
Tim Delaney