Super() function

Discussion in 'Python' started by Alan Harris-Reid, Mar 25, 2010.

  1. Hi,

    Using Python 3.1, I sometimes use the super() function to call the
    equivalent method from a parent class, for example

    def mymethod(self):
    super().mymethod()
    some more code...

    Is there any way of writing the code so that the super() call is generic
    and automatically recognises the name of the current method (ie.
    something like super().thismethod()) or do I always have to repeat the
    method name after super()?

    TIA,
    Alan
     
    Alan Harris-Reid, Mar 25, 2010
    #1
    1. Advertising

  2. Alan Harris-Reid

    alex23 Guest

    Alan Harris-Reid <> wrote:
    > Is there any way of writing the code so that the super() call is generic
    > and automatically recognises the name of the current method (ie.
    > something like super().thismethod()) or do I always have to repeat the
    > method name after super()?


    To the best of my knowledge, you always need to repeat the method
    name. super() returns a proxy object that has no awareness of its
    context, so it's unaware of the method it's being called within (in
    fact, there's nothing that restricts super() to only being used in
    methods...).

    You could probably write a wrapper function that uses inspect to
    determine in which context super() is being called, but unless you're
    regularly finding this to be a problem with refactoring I wouldn't
    worry about it.
     
    alex23, Mar 25, 2010
    #2
    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,845
    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:
    739
    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:
    485
    Michele Simionato
    Jan 15, 2004
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. GOOD LUCK GROUP
    Replies:
    0
    Views:
    409
    GOOD LUCK GROUP
    Apr 28, 2008
Loading...

Share This Page