[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Super() function

Alan Harris-Reid

3/25/2010 3:18:00 AM

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
1 Answer

alex23

3/25/2010 3:47:00 AM

0

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.