473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
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
Jul 18 '05 #1
3 1773
Gonçalo Rodrigues <op*****@mail.telepac.pt> 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
Jul 18 '05 #2
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.
Jul 18 '05 #3

"Harry Pehkonen" <ha************@hotpop.com> wrote in message
news:70**************************@posting.google.c 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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kerim Borchaev | last post by:
Hello! Always when I use "super" I create a code duplication because class used as first arg to "super" is always the class where the method containing "super" was defined in: ''' class C:...
2
by: Clarence Gardner | last post by:
The super object is considered a solution to the "diamond problem". However, it generally requires that the ultimate base class know that it is last in the method resolution order, and hence it...
11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
9
by: Paul Rubin | last post by:
I'm trying the super() function as described in Python Cookbook, 1st ed, p. 172 (Recipe 5.4). class A(object): def f(self): print 'A' class B(object): def f(self):
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
1
by: Adam Monsen | last post by:
Is there anything wrong with using something like super(type(self), self).f() to avoid having to hardcode a type? For example: class A(object): def f(self): print "in A.f()" class B(A): def...
1
by: Alex Greif | last post by:
Hi, Before 2.2 I could initialize multiple super classes like this: class B(A,AA): def __init__(self, args): A.__init__(self,args) AA.__init__(self,args) Tutorials say that since python 2.2...
6
by: David Hirschfield | last post by:
I'm having trouble with the new descriptor-based mechanisms like super() and property() stemming, most likely, from my lack of knowledge about how they work. Here's an example that's giving me...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.