super question

  • Thread starter Gonçalo Rodrigues
  • Start date
G

Gonçalo Rodrigues

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:
.... def test(self):
.... return "I'm %r." % self
.... .... def test(self):
.... return "I'm a no one."
.... 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
<bound method Test2.test of <class '__main__.Test2'>>

Huh? shouldn't it return the *unbound* method test at class Test? And
more:
Traceback (most recent call last):

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

With my best regards,
G. Rodrigues
 
M

Michael Hudson

Gonçalo Rodrigues said:
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
 
H

Harry Pehkonen

Sorry -- Google is not letting me reply to the correct posting . . .
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.
 
T

Terry Reedy

Harry Pehkonen said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,104
Messages
2,570,646
Members
47,248
Latest member
Angelita78

Latest Threads

Top