Super() function

A

Alan Harris-Reid

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
 
A

alex23

Alan Harris-Reid said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top