473,473 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

in search of a nicer super ...

Here is an idea for a nicer syntax in cooperative method calls,
which is not based on Guido's "autosuper" example. This is just a
hack, waiting for a nicer "super" built-in ...

Here is example of usage:

from cooperative import Cooperative

class B(Cooperative):
def print_(self):
print "B",

class C(B):
def print_(self, super):
super.print_() # shortcut for super(C,self).print_()
print "C",

class D(C):
def print_(self, super):
super.print_() # shortcut for super(D,self).print_()
print "D",

D().print_() # prints BCD

Let me call "cooperative methods" methods with a second argument
called "super": then super.method(*args, **kw) acts as a shortcut
for super(cls,self).method(*args, **kw). This avoids the redundant
repetition of the class in the super object, a thing I always loathed :)

Here is the "cooperative" module:

$ cat cooperative.py
import inspect

def second_arg(func):
args = inspect.getargspec(func)[0]
if len(args) >= 2: return args[1]

class _Cooperative(type):
def __init__(cls,name,bases,dic):
for n,func in dic.iteritems():
if inspect.isfunction(func) and second_arg(func) == "super":
setattr(cls, n, cls.wrap(func))
def wrap(cls, func):
return lambda self, *args, **kw : \
func(self, super(cls, self), *args, **kw)

class Cooperative:
__metaclass__ = _Cooperative

I could make it to work for staticmethods and classmethods, but
I didn't bothered since I wanted to keep the cooperative module
under the twenty lines.

Just my 0.02c,
Michele Simionato
Jul 18 '05 #1
0 1223

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

Similar topics

4
by: Kerim Borchaev | last post by:
Hello! Always when I use "super" I create a code duplication because class used as first arg to "super" is always the class where the method containing "super" was defined in: ''' class C:...
11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
6
by: Steven Bethard | last post by:
When would you call super with only one argument? The only examples I can find of doing this are in the test suite for super. Playing around with it: py> class A(object): .... x = 'a'...
3
by: jcouse | last post by:
I am trying to find if a string exists in a file. If it doesn’t, it should return a “-1” and I’ll make my decision based on that Here is a sniplet of the text file game name mapp...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
7
by: Pupeno | last post by:
Hello, I have a class called MyConfig, it is based on Python's ConfigParser.ConfigParser. It implements add_section(self, section), which is also implemented on ConfigParser.ConfigParser, which I...
9
by: Mike Krell | last post by:
I'm reading Alex Martelli's "Nutshell" second edition. In the section called "Cooperative superclass method calling", he presents a diamond inheritance hierachy: class A(object): def...
6
by: MLH | last post by:
I'm sure its a bozo question, but it's got me stumped. How do I search for a question mark in an open table using CTRL-F to launch the search in the current field. The field is a text field. Not...
1
by: duzhidian | last post by:
Dear All: I need write a php program online to implement a super search engine, for example, when users input some keyword, my program can search different other online search engines and reply...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.