473,382 Members | 1,720 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,382 software developers and data experts.

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

Apr 4 '07 #1
10 13268
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
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
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
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
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
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

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
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
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
John Clark a écrit :
Yeah!!! One I can actually answer!!!
+1 QOTW
Apr 4 '07 #11

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

Similar topics

4
by: Martin Maney | last post by:
I've been to the cookbook (both forms, since they're sometimes usefully different), but that gave rise to more questions than answers. Heck, let me pull some of them up while I'm at it. ...
6
by: Steven Bethard | last post by:
So when I'm writing a class and I define an __init__ method, I sometimes haven't called object.__init__, e.g.: class C(object): def __init__(self, x): self.x = x instead of class...
8
by: Steven Bethard | last post by:
I'm playing around with some threading stuff right now, and I'm having a little trouble calling a function from one thread that affects another. Here's my setup: py> import os, threading, time...
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
5
by: Da Costa Gomez | last post by:
Hi, I was wondering whether someone could shed some light on the following. Using inheritance in Java one can override a function f() (or is it overload?) in the child and then do: public f() {...
12
by: torbs | last post by:
Hi I have a a function with several methods. For simplicity it looks a bit like this: super.prototype.aProperty="HELLO"; super.prototype.returnValue = function () { return 2;
1
by: Florian Lindner | last post by:
Hello, I try to call the superclass of the ConfigParser object: class CustomizedConfParser(ConfigParser.SafeConfigParser): def get(self, section, attribute): try: return...
20
by: Jorgen Bodde | last post by:
Hi All, Now that I am really diving into Python, I encounter a lot of things that us newbies find difficult to get right. I thought I understood how super() worked, but with 'private' members it...
4
by: David Given | last post by:
(No, I'm *not* trying to call a constructor from another constructor!) I have this class: class P : public std::stringstream { public: ~P() { std::cout << str(); } };
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.