in search of a nicer super ...

Discussion in 'Python' started by Michele Simionato, May 18, 2004.

  1. 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
    Michele Simionato, May 18, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,737
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    708
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    466
    Michele Simionato
    Jan 15, 2004
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. GOOD LUCK GROUP
    Replies:
    0
    Views:
    384
    GOOD LUCK GROUP
    Apr 28, 2008
Loading...

Share This Page