Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > super question

Reply
Thread Tools

super question

 
 
Gonšalo Rodrigues
Guest
Posts: n/a
 
      09-01-2003
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
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      09-01-2003
Gonšalo Rodrigues <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Harry Pehkonen
Guest
Posts: n/a
 
      09-01-2003
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.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-01-2003

"Harry Pehkonen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why super.super is not allowed$B!)(B bearice@gmail.com Java 7 04-06-2008 01:11 AM
super.super.super how? Java 24 02-24-2005 10:51 PM
super. could there be a simpler super? Kerim Borchaev Python 4 01-15-2004 03:15 PM
WHICH LENS? - SIGMA 70-300mm DL MACRO Super-II or 28-300mm AF Super Zoom 4 - for Canon 300d Rebel Digital SLR? No Spam Digital Photography 24 11-27-2003 08:21 AM
Getting the super class via the super() function Fernando Rodriguez Python 2 11-22-2003 12:08 AM



Advertisments