super question

Discussion in 'Python' started by Gonçalo Rodrigues, Sep 1, 2003.

  1. Hi,

    Ok, now I'm really confused. What is supposed

    super(<class>, <subclass of class>)

    to do?

    My thought was that with the following setup:

    >>> class Test(object):

    .... def test(self):
    .... return "I'm %r." % self
    ....
    >>> class Test2(Test):

    .... def test(self):
    .... return "I'm a no one."
    ....
    >>> super(Test, Test2).test

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    AttributeError: 'super' object has no attribute 'test'

    So far so good, object class (the super class of Test) defines no test
    method so it barfs. But

    >>> super(Test2, Test2).test

    <bound method Test2.test of <class '__main__.Test2'>>

    Huh? shouldn't it return the *unbound* method test at class Test? And
    more:

    >>> a = Test2()
    >>> super(Test2, Test2).test(a)

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    TypeError: test() takes exactly 1 argument (2 given)
    >>> super(Test2, Test2).test()

    "I'm <class '__main__.Test2'>."
    >>>


    A bug? Or my perceptions on what super(<class>, <subclass of class>)
    should do are totally mixed up?

    With my best regards,
    G. Rodrigues
     
    Gonçalo Rodrigues, Sep 1, 2003
    #1
    1. Advertising

  2. Gonçalo Rodrigues <> writes:

    > Hi,
    >
    > Ok, now I'm really confused. What is supposed
    >
    > super(<class>, <subclass of class>)
    >
    > to do?


    My *guess* (and it is very much a guess) is that super(<class>,
    <subclass of class>) is intended to be used in classmethods.

    Cheers,
    mwh

    --
    Wise frogs would take sanctuary in the cool moistness of the cat's
    milk-dish, from where they would watch you. It's worrying being
    watched by a milk-dish. -- Tanuki the Raccoon-dog, asr
     
    Michael Hudson, Sep 1, 2003
    #2
    1. Advertising

  3. Sorry -- Google is not letting me reply to the correct posting . . .

    Aahz wrote:
    > > but why not just do:
    > >
    > > class C(B):
    > > def meth(self, arg):
    > > B.meth(self, arg)
    > >
    > >Is there any difference?
    > >Any advantage either way?


    > The advantage comes when you rename B or when you have multiple base
    > classes for C.


    Like this?:

    class C(A, B):
    def __init__(self):
    for parent in C.__bases__:
    parent.__init__(self)

    I don't think there is one definitive answer to the question of how to do this.

    Harry.
     
    Harry Pehkonen, Sep 1, 2003
    #3
  4. Gonçalo Rodrigues

    Terry Reedy Guest

    "Harry Pehkonen" <> wrote in message
    news:...
    > Aahz wrote:
    > > > but why not just do:
    > > >
    > > > class C(B):
    > > > def meth(self, arg):
    > > > B.meth(self, arg)
    > > >
    > > >Is there any difference?
    > > >Any advantage either way?

    >
    > > The advantage comes when you rename B or when you have multiple

    base
    > > classes for C.

    >
    > Like this?:
    >
    > class C(A, B):
    > def __init__(self):
    > for parent in C.__bases__:
    > parent.__init__(self)


    If A and B have common ancestor D, and both have code like either of
    above, then above will result in two calls to D.__init__, once before
    and once after B.__init__. I believe point of super mechanism is to
    have D.__init__ called once at the proper time.

    TJR
     
    Terry Reedy, Sep 1, 2003
    #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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,823
    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:
    728
    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:
    482
    Michele Simionato
    Jan 15, 2004
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. GOOD LUCK GROUP
    Replies:
    0
    Views:
    405
    GOOD LUCK GROUP
    Apr 28, 2008
Loading...

Share This Page