super question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gonçalo Rodrigues

    super question

    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:
    [color=blue][color=green][color=darkred]
    >>> class Test(object):[/color][/color][/color]
    .... def test(self):
    .... return "I'm %r." % self
    ....[color=blue][color=green][color=darkred]
    >>> class Test2(Test):[/color][/color][/color]
    .... def test(self):
    .... return "I'm a no one."
    ....[color=blue][color=green][color=darkred]
    >>> super(Test, Test2).test[/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve 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
    [color=blue][color=green][color=darkred]
    >>> super(Test2, Test2).test[/color][/color][/color]
    <bound method Test2.test of <class '__main__.Test2 '>>

    Huh? shouldn't it return the *unbound* method test at class Test? And
    more:
    [color=blue][color=green][color=darkred]
    >>> a = Test2()
    >>> super(Test2, Test2).test(a)[/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: test() takes exactly 1 argument (2 given)[color=blue][color=green][color=darkred]
    >>> super(Test2, Test2).test()[/color][/color][/color]
    "I'm <class '__main__.Test2 '>."[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

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

    With my best regards,
    G. Rodrigues
  • Michael Hudson

    #2
    Re: super question

    Gonçalo Rodrigues <op73418@mail.t elepac.pt> writes:
    [color=blue]
    > Hi,
    >
    > Ok, now I'm really confused. What is supposed
    >
    > super(<class>, <subclass of class>)
    >
    > to do?[/color]

    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

    Comment

    • Harry Pehkonen

      #3
      Re: super question

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

      Aahz wrote:[color=blue][color=green]
      > > but why not just do:
      > >
      > > class C(B):
      > > def meth(self, arg):
      > > B.meth(self, arg)
      > >
      > >Is there any difference?
      > >Any advantage either way?[/color][/color]
      [color=blue]
      > The advantage comes when you rename B or when you have multiple base
      > classes for C.[/color]

      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.

      Comment

      • Terry Reedy

        #4
        Re: super question


        "Harry Pehkonen" <harry.pehkonen @hotpop.com> wrote in message
        news:70df36e9.0 309011030.4de74 e3b@posting.goo gle.com...[color=blue]
        > Aahz wrote:[color=green][color=darkred]
        > > > but why not just do:
        > > >
        > > > class C(B):
        > > > def meth(self, arg):
        > > > B.meth(self, arg)
        > > >
        > > >Is there any difference?
        > > >Any advantage either way?[/color][/color]
        >[color=green]
        > > The advantage comes when you rename B or when you have multiple[/color][/color]
        base[color=blue][color=green]
        > > classes for C.[/color]
        >
        > Like this?:
        >
        > class C(A, B):
        > def __init__(self):
        > for parent in C.__bases__:
        > parent.__init__ (self)[/color]

        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


        Comment

        Working...