473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

supermethod shortcut

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
Jul 18 '05 #1
3 1412
"David Fraser" <da****@sjsoft.com> wrote in message
news:c9**********@ctb-nnrp2.saix.net...
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/Coo.../Recipe/284528

Jul 18 '05 #2
huy
Sean Ross wrote:
"David Fraser" <da****@sjsoft.com> wrote in message
news:c9**********@ctb-nnrp2.saix.net...
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/Coo.../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
Jul 18 '05 #3
huy <ny*****@swiftdsl.com.au> wrote in message news:<40**********************@news.syd.swiftdsl.c om.au>...

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/descrint...l#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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: aa | last post by:
Thought this question might be out of this NG's scope, there are always knowledgable people who might hava an answer. A hyperlink to a shortcut to a file returnes an empty screen, and the source...
10
by: Lex | last post by:
I am writing a C# app that has a Menu. Some of the menu items will have short cuts that do not exist in the Shortcut enum. I would like the custom shortcuts to appear on the menu but as far as I...
4
by: Salad | last post by:
A97. If you set a forms properties to ShortcutMenu = No, the entire form is disabled from displaying a shortcut menu. If you set the property to Yes, all controls have a shortcut menu. I have...
0
by: cefrancke | last post by:
I recently discovered, that if you set the startup options for "security", you will have alot of work do to get Access back to "normal". If you disable the built-in menus/toolbars you'll have to...
0
by: Marcel | last post by:
Hi, the application I am writing has a MainMenu and a DataGrid (UltraGrid). Now I have to edit the text of each cell in this grid. So far so good. All works fine in editmode but if I press a...
4
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
3
by: TC | last post by:
I've written an application using VB.NET 2003 and packaged it with a deployment project. When I use the setup program to install the application, however, it doesn't work. More specifically, the...
7
by: GrandpaB | last post by:
I would appreciate assistance learning how to create a Desktop shortcut in my setup project. In the left pane of the Setup/File System window I right-clicked User's Desktop. From the contex...
5
by: remya1000 | last post by:
i'm using VB.NET 2003 application program. by using OpenFileDialog, we can select the file name or file path. OpenFileDialog1.ShowDialog() pgmPath.Text =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.