supermethod shortcut

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Fraser

    supermethod shortcut

    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(sel f, arg1, arg2):
    do_something(ar g1)
    arg2 += 1
    super(Y, self).some_meth od(arg1, arg2)

    The whole super(Y, self).some_meth od 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(cur rentclass):
    callingframe = sys._getframe() .f_back
    functionname = callingframe.f_ code.co_name
    self = callingframe.f_ locals["self"]
    superobject = super(currentcl ass, self)
    return getattr(superob ject, 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(cur rentclass, self):
    callingframe = sys._getframe() .f_back
    functionname = callingframe.f_ code.co_name
    superobject = super(currentcl ass, self)
    return getattr(superob ject, functionname)

    This still means you call supermethod(Y, self)(arg1, arg2) instead of
    super(Y, self).functionn ame(arg1, arg2)

    Anyway I just wondered if others would find this interesting / useful /
    material for a flame war :-)

    David
  • Sean Ross

    #2
    Re: supermethod shortcut

    "David Fraser" <davidf@sjsoft. com> wrote in message
    news:c91uk9$krq $1@ctb-nnrp2.saix.net. ..[color=blue]
    > Hi
    >
    > I was recently thinking about the awkwardness I find in using the super
    > method to call a superclass's function.
    >[/color]
    [snip][color=blue]
    >
    > The whole super(Y, self).some_meth od seems an awkward way to say "call
    > the superclass's version of this function".[/color]
    [snip]


    You may find the following recipe interesting:





    Comment

    • huy

      #3
      Re: supermethod shortcut

      Sean Ross wrote:[color=blue]
      > "David Fraser" <davidf@sjsoft. com> wrote in message
      > news:c91uk9$krq $1@ctb-nnrp2.saix.net. ..
      >[color=green]
      >>Hi
      >>
      >>I was recently thinking about the awkwardness I find in using the super
      >>method to call a superclass's function.
      >>[/color]
      >
      > [snip]
      >[color=green]
      >>The whole super(Y, self).some_meth od seems an awkward way to say "call
      >>the superclass's version of this function".[/color]
      >
      > [snip]
      >
      >
      > You may find the following recipe interesting:
      >
      > http://aspn.activestate.com/ASPN/Coo.../Recipe/284528
      >[/color]

      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


      Comment

      • Michele Simionato

        #4
        Re: supermethod shortcut

        huy <nytimes@swiftd sl.com.au> wrote in message news:<40b6c250$ 0$2299$61ce578d @news.syd.swift dsl.com.au>...[color=blue]
        >
        > 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[/color]

        From http://www.python.org/2.2.2/descrint...l#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

        Comment

        Working...