Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Super() function

1 view
Skip to first unread message

Alan Harris-Reid

unread,
Mar 24, 2010, 11:17:52 PM3/24/10
to Python List
Hi,

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

alex23

unread,
Mar 24, 2010, 11:47:19 PM3/24/10
to
Alan Harris-Reid <aharrisr...@googlemail.com> wrote:
> 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()?

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.

Gabriel Genellina

unread,
Mar 25, 2010, 12:00:27 AM3/25/10
to pytho...@python.org
En Thu, 25 Mar 2010 00:17:52 -0300, 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...
>

> 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

Alan Harris-Reid

unread,
Mar 27, 2010, 12:43:11 PM3/27/10
to Gabriel Genellina, Python List
Gabriel Genellina wrote:
> <div class="moz-text-flowed">En Thu, 25 Mar 2010 00:17:52 -0300, 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...
>>
>> 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.

Regards,
Alan

Delaney, Timothy (Tim)

unread,
Mar 28, 2010, 8:58:07 PM3/28/10
to Python List
> 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...
>>>
>>> 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

Gabriel Genellina

unread,
Mar 29, 2010, 3:19:18 PM3/29/10
to pytho...@python.org
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 :(

--
Gabriel Genellina

Delaney, Timothy (Tim)

unread,
Mar 29, 2010, 7:35:43 PM3/29/10
to Gabriel Genellina, pytho...@python.org
Gabriel Genellina write:

> 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

0 new messages