473,796 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ 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).p rint_()
print "C",

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

D().print_() # prints BCD

Let me call "cooperativ e methods" methods with a second argument
called "super": then super.method(*a rgs, **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 "cooperativ e" module:

$ cat cooperative.py
import inspect

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

class _Cooperative(ty pe):
def __init__(cls,na me,bases,dic):
for n,func in dic.iteritems() :
if inspect.isfunct ion(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 1241

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

Similar topics

4
2333
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: def method(self): super(C, self).method() '''
11
5037
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 function on pages 89-90. Based on the example on page 90, I wrote this test code : class A(object): def test(self): print 'A'
6
2016
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' .... py> class B(A): .... x = 'b' ....
3
1287
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 description "Mappy (US) year 198 manufacturer "Namco history "\nMappy (c) 03/1983 Namco. \n\n- TRIVIA: \n\nLicensed to Bally Midway for US manufacture and distribution. (03/1983) \n\n- SERIES: \n\n1. Mappy \n2. Hopping Mappy \n\n0.26 \n\nBugs: \n-...
7
5261
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 inheritance when the base classes are written to expect it, so "Always use super()" seems like bad advice. OTOH sometimes you need super() to get correct behaviour. ISTM "Only use super() when you know you need it" might be
7
1933
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 want to call. So, reducing the problem to the bare minimum, the class (with a useless add_section that shows the problem): .... def add_section(self, section): .... super(MyConfig, self).add_section(section)
9
2204
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 met(self): print "A.met" class B(A): def met(self): print "B.met"
6
2675
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 all records have a "?" in the field, but some do. The search process treats the question-mark as a wildcard character and finds the occurrence in EVERY record. Hmmm??? Is there no way to specify a Chr$(63)
1
2049
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 user some unified results from these search engines. For example, let users input some cities into my web page and it will query
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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 we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.