supermethod shortcut

Discussion in 'Python' started by David Fraser, May 26, 2004.

  1. David Fraser

    David Fraser Guest

    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
     
    David Fraser, May 26, 2004
    #1
    1. Advertising

  2. David Fraser

    Sean Ross Guest

    "David Fraser" <> wrote in message
    news:c91uk9$krq$...
    > 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
     
    Sean Ross, May 26, 2004
    #2
    1. Advertising

  3. David Fraser

    huy Guest

    Sean Ross wrote:
    > "David Fraser" <> wrote in message
    > news:c91uk9$krq$...
    >
    >>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
     
    huy, May 28, 2004
    #3
  4. huy <> wrote in message news:<40b6c250$0$2299$>...
    >
    > 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
     
    Michele Simionato, May 28, 2004
    #4
    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. Gilles LAMBERT
    Replies:
    1
    Views:
    479
    Mattias Sjögren
    Sep 22, 2003
  2. zeeshan.asghar

    getting path from shortcut

    zeeshan.asghar, Jan 12, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,199
    Michael Nemtsev
    Jan 12, 2006
  3. Stoyan Stratev

    shortcut to web application

    Stoyan Stratev, Jun 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,280
    Predrag
    Jun 30, 2003
  4. Chetna Joshi
    Replies:
    0
    Views:
    507
    Chetna Joshi
    Jul 5, 2003
  5. Anita C

    Resolving a shortcut

    Anita C, Nov 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    424
    Guest
    Nov 8, 2003
Loading...

Share This Page