473,881 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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><BOD Y>";
print(self.text );
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text );

class NewPage(HTMLMai n):
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 13316
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.or g
[mailto:py****** *************** ************@py thon.org] On Behalf Of
Fi************@ gmail.com
Sent: Wednesday, April 04, 2007 3:23 PM
To: py*********@pyt hon.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><BOD Y>";
print(self.text );
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text );

class NewPage(HTMLMai n):
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><BOD Y>";
print(self.text );
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text );

class NewPage(HTMLMai n):
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><BOD Y>";
print(self.text );
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text );

class NewPage(HTMLMai n):
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><BOD Y>";
print(self.text );
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text );

class NewPage(HTMLMai n):
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,*ar gs):
super(B,self).m ethod(*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).m ethod(*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.or g
[mailto:py****** *************** ************@py thon.org] On Behalf Of Laszlo
Nagy
Sent: Wednesday, April 04, 2007 4:09 PM
To: Jarek Zgoda; py*********@pyt hon.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><BOD Y>";
print(self.text );
def __del__(self):
self.text = "</BODY></HTML>";
print(self.text );

class NewPage(HTMLMai n):
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,*ar gs):
super(B,self).m ethod(*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).m ethod(*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(HTMLMai n):
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

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

Similar topics

4
5166
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. Martelli gives the recipie (5.3 on paper) "Calling a Superclass __init__ Method if it Exists" where he seems to say that this: class NewStyleOnly(A, B, C): def __init__(self): super(NewStyleOnly, self).__init__()
6
3722
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 C(object):
8
1896
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 py> def write(file_in, input_lines): .... for line in input_lines: .... time.sleep(0.5) .... file_in.write(line) .... file_in.flush()
14
6416
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 is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
5
6543
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() { super.f(); ... } in the child to first execute the parent stuff to be followed by the
12
1748
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
2103
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 super(CustomizedConfParser, self).get(section, attribute) #
20
2877
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 does not seem to work. For example; .... def __baseMethod(self): .... print 'Test'
4
1725
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
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10401
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9554
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.