calling super()

F

Finger.Octopus

Hello, I have been trying to call the super constructor from my
derived class but its not working as expected. See the code:

class HTMLMain:
def __init__(self):
self.text = "<HTML><BODY>";
print(self.text);
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text);

class NewPage(HTMLMain):
def __init__(self):
print 'derive2 init'
super(NewPage, self).__init__();


N = NewPage();
del N


And here's the error message I get:

Traceback (most recent call last):
File "e:/PyEN/inherit.py", line 16, in <module>
N = NewPage();
File "e:/PyEN/inherit.py", line 12, in __init__
super(NewPage, self).__init__();
TypeError: super() argument 1 must be type, not classobj
 
L

Laszlo Nagy

And here's the error message I get:

Traceback (most recent call last):
File "e:/PyEN/inherit.py", line 16, in <module>
N = NewPage();
File "e:/PyEN/inherit.py", line 12, in __init__
super(NewPage, self).__init__();
TypeError: super() argument 1 must be type, not classobj
Super works correctly for new-style classes only. Try to inherit from
"object".

class HTMLMain(object):




Best,

Laszlo
 
J

John Clark

Yeah!!! One I can actually answer!!!

super() only works on new-style classes - your classes don't have object
anywhere in the inheritance tree so super() isn't going to help..

New-style classes are known as types, old-style classes are known as
classobjs.

Hope this helps,
-jdc


-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On Behalf Of
(e-mail address removed)
Sent: Wednesday, April 04, 2007 3:23 PM
To: (e-mail address removed)
Subject: calling super()

Hello, I have been trying to call the super constructor from my derived
class but its not working as expected. See the code:

class HTMLMain:
def __init__(self):
self.text = "<HTML><BODY>";
print(self.text);
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text);

class NewPage(HTMLMain):
def __init__(self):
print 'derive2 init'
super(NewPage, self).__init__();


N = NewPage();
del N


And here's the error message I get:

Traceback (most recent call last):
File "e:/PyEN/inherit.py", line 16, in <module>
N = NewPage();
File "e:/PyEN/inherit.py", line 12, in __init__
super(NewPage, self).__init__();
TypeError: super() argument 1 must be type, not classobj
 
J

Jarek Zgoda

(e-mail address removed) napisa³(a):
Hello, I have been trying to call the super constructor from my
derived class but its not working as expected. See the code:

class HTMLMain:
def __init__(self):
self.text = "<HTML><BODY>";
print(self.text);
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text);

class NewPage(HTMLMain):
def __init__(self):
print 'derive2 init'
super(NewPage, self).__init__();

This should read: super(HTMLMain, self).__init__()

I think it's just a quick-type-typo. ;)
 
A

attn.steven.kuo

Hello, I have been trying to call the super constructor from my
derived class but its not working as expected. See the code:

class HTMLMain:
def __init__(self):
self.text = "<HTML><BODY>";
print(self.text);
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text);

class NewPage(HTMLMain):
def __init__(self):
print 'derive2 init'
super(NewPage, self).__init__();

N = NewPage();
del N

And here's the error message I get:


The error message is trying to tell
you that 'super' should be used with
new style classes.

So,

class HTMLMain:

should be

class HTMLMain(object):
 
L

Laszlo Nagy

Jarek said:
This should read: super(HTMLMain, self).__init__()
Definitely, this is not true. Well, it depends what the OP wanted to do
here, but in 99.9% of the cases, you want to use

class B(A):
def method(self,*args):
super(B,self).method(*args)

Look at here: http://www.python.org/doc/2.3.5/lib/built-in-funcs.html#l2h-66
It tells that super(type,[self]) will """Return the superclass of
type.""". So super(B) will return the superclass of B, that is A. The
built-in function "super" is very useful when you have diamond-shaped
inheritance and you only want each inherited method to be called only
once, IN THE CORRECT ORDER. If you only have single inheritance class
trees, then super(B,self).method(*args) is identical to
A.method(self,*args). You only need to worry about method calling order
when you use multiple inheritance. However, using super is much nicer
than calling the method of the base class directly, and it is
syntactically cleaner, since you will only have a single reference to
the base class, in the class definition header. (E.g. you can change the
base class by replacing one word in the source code...)

Best,

Laszlo
 
J

John Clark

Please be aware that super() has it's own set of gotchas - it's not as clean
as you would hope. For more info: http://fuhm.org/super-harmful/

(I'm not the author, I was referred to this article while struggling with
wxPython and super())

-John Clark

-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On Behalf Of Laszlo
Nagy
Sent: Wednesday, April 04, 2007 4:09 PM
To: Jarek Zgoda; (e-mail address removed); (e-mail address removed)
Subject: Re: calling super()

Jarek said:
This should read: super(HTMLMain, self).__init__()
Definitely, this is not true. Well, it depends what the OP wanted to do
here, but in 99.9% of the cases, you want to use

class B(A):
def method(self,*args):
super(B,self).method(*args)

Look at here: http://www.python.org/doc/2.3.5/lib/built-in-funcs.html#l2h-66
It tells that super(type,[self]) will """Return the superclass of type.""".
So super(B) will return the superclass of B, that is A. The built-in
function "super" is very useful when you have diamond-shaped inheritance and
you only want each inherited method to be called only once, IN THE CORRECT
ORDER. If you only have single inheritance class trees, then
super(B,self).method(*args) is identical to A.method(self,*args). You only
need to worry about method calling order when you use multiple inheritance.
However, using super is much nicer than calling the method of the base class
directly, and it is syntactically cleaner, since you will only have a single
reference to the base class, in the class definition header. (E.g. you can
change the base class by replacing one word in the source code...)

Best,

Laszlo
 
J

Jarek Zgoda

Laszlo Nagy napisa³(a):
Definitely, this is not true. Well, it depends what the OP wanted to do
here, but in 99.9% of the cases, you want to use

Hah! I cancelled this message but seconds too late...
 
B

Bruno Desthuilliers

Jarek Zgoda a écrit :
(e-mail address removed) napisa³(a):

(snip)



This should read: super(HTMLMain, self).__init__()

Nope. It's just how it should be.
 

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
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top