supermethod shortcut

D

David Fraser

Hi

I was recently thinking about the awkwardness I find in using the super
method to call a superclass's function.

The majority of the time I use super in the following way:

class Y(X):
def some_method(self, arg1, arg2):
do_something(arg1)
arg2 += 1
super(Y, self).some_method(arg1, arg2)

The whole super(Y, self).some_method seems an awkward way to say "call
the superclass's version of this function".

So I defined a function that looks up the calling function name in the
stack frame and calls super on the self attribute, and returns the
function name:

import sys

def supermethod(currentclass):
callingframe = sys._getframe().f_back
functionname = callingframe.f_code.co_name
self = callingframe.f_locals["self"]
superobject = super(currentclass, self)
return getattr(superobject, functionname)

This then reduces the above to supermethod(Y)(arg1, arg2)

Of course explicit is better than implicit and so maybe not looking up
self is better:

import sys

def supermethod(currentclass, self):
callingframe = sys._getframe().f_back
functionname = callingframe.f_code.co_name
superobject = super(currentclass, self)
return getattr(superobject, functionname)

This still means you call supermethod(Y, self)(arg1, arg2) instead of
super(Y, self).functionname(arg1, arg2)

Anyway I just wondered if others would find this interesting / useful /
material for a flame war :)

David
 
H

huy

Sean said:
Hi

I was recently thinking about the awkwardness I find in using the super
method to call a superclass's function.

[snip]

The whole super(Y, self).some_method seems an awkward way to say "call
the superclass's version of this function".

[snip]


You may find the following recipe interesting:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/284528

This recipe is nice. It would be nicer if it became the standard when
doing OOP in python. I always disliked the current super call in python;
too much implementation detail in the syntax IMO.

Huy
 
M

Michele Simionato

huy said:
This recipe is nice. It would be nicer if it became the standard when
doing OOP in python. I always disliked the current super call in python;
too much implementation detail in the syntax IMO.

Huy

From http://www.python.org/2.2.2/descrintro.html#cooperation:

"""
The super call as shown above is somewhat prone to errors: it is easy
to copy and paste a super call from one class to another while
forgetting to change the class name to that of the target class, and
this mistake won't be detected if both classes are part of the same
inheritance graph. (You can even cause infinite recursion by
mistakenly passing in the name of a derived class of the class
containing the super call.) It would be nice if we didn't have to name
the class explicitly, but this would require more help from Python's
parser than we can currently get. I hope to fix this in a future
Python release by making the parser recognize super.
"""

This is Guido's own saying.

Michele Simionato
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top