By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

calling super()

P: n/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__();
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

Apr 4 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Apr 4 '07 #2

P: n/a
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: py*********************************@python.org
[mailto:py*********************************@python. org] On Behalf Of
Fi************@gmail.com
Sent: Wednesday, April 04, 2007 3:23 PM
To: py*********@python.org
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

--
http://mail.python.org/mailman/listinfo/python-list

Apr 4 '07 #3

P: n/a
Fi************@gmail.com 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. ;)

--
Jarek Zgoda
http://jpa.berlios.de/
Apr 4 '07 #4

P: n/a
On Apr 4, 12:22 pm, Finger.Octo...@gmail.com wrote:
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):

--
Hope this helps,
Steven

Apr 4 '07 #5

P: n/a
John Clark wrote:
Yeah!!! One I can actually answer!!!
Nice to see such enthusiasm. Well done!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 4 '07 #6

P: n/a
Jarek Zgoda wrote:
>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__()
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/...cs.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
Apr 4 '07 #7

P: n/a

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: py*********************************@python.org
[mailto:py*********************************@python. org] On Behalf Of Laszlo
Nagy
Sent: Wednesday, April 04, 2007 4:09 PM
To: Jarek Zgoda; py*********@python.org; Fi************@gmail.com
Subject: Re: calling super()

Jarek Zgoda wrote:
>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__()
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/...cs.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
--
http://mail.python.org/mailman/listinfo/python-list

Apr 4 '07 #8

P: n/a
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...

--
Jarek Zgoda
http://jpa.berlios.de/
Apr 4 '07 #9

P: n/a
Jarek Zgoda a écrit :
Fi************@gmail.com napisa³(a):

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


This should read: super(HTMLMain, self).__init__()
Nope. It's just how it should be.

Apr 4 '07 #10

P: n/a
John Clark a écrit :
Yeah!!! One I can actually answer!!!
+1 QOTW
Apr 4 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.