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. Advertisements

  2. David Fraser

    Sean Ross Guest

    [snip]


    You may find the following recipe interesting:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/284528
     
    Sean Ross, May 26, 2004
    #2
    1. Advertisements

  3. David Fraser

    huy Guest

    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. 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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.