Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser
__init__(...) of the superclass Exception with 'super(..).__init__(..)' .
However, trying to do so results in a
'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
works just as one would expect.
Why does 'super(..).__init__(..)' fail?
thank you for any suggestions
chriss
Here is some example code to illustrate the point:
class WorkingException(Exception):
def __init__(self, message):
# works as I would expect
Exception.__init__(self, message)
class BrokenException(Exception):
def __init__(self, message):
# fails with a typeError
super(BrokenException, self).__init__(self, message)
# --------- case 1 ---------
try:
raise WorkingException("Hello WorkingException")
except WorkingException, e:
print e
# --------- case 3 ---------
try:
raise BrokenException("Hello BrokenException")
except BrokenException, e:
print e 3 5123
chriss wrote: Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser __init__(...) of the superclass Exception with 'super(..).__init__(..)' . However, trying to do so results in a 'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything works just as one would expect.
Why does 'super(..).__init__(..)' fail?
Exceptions do not inherit from 'object'; they are old-style classes.
super() can be used only with new-style classes (which subclass 'object').
-Peter
chriss wrote: Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser __init__(...) of the superclass Exception with 'super(..).__init__(..)' . However, trying to do so results in a 'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything works just as one would expect.
Why does 'super(..).__init__(..)' fail?
AFAICT, the Exception hierarchy are still old-style classes while
super() only works on new-style classes.
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Peter Hansen wrote: chriss wrote: Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser __init__(...) of the superclass Exception with 'super(..).__init__(..)' . However, trying to do so results in a 'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything works just as one would expect.
Why does 'super(..).__init__(..)' fail?
Exceptions do not inherit from 'object'; they are old-style classes.
super() can be used only with new-style classes (which subclass 'object').
-Peter
That explains it all right.
Thank you very much for your answer.
chriss This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: daishi |
last post by:
Hi,
I am wondering if someone could help me understand some of the
details of using metaclasses.
I am trying to use metaclasses to auto-generate some attributes
and methods in a class...
|
by: Chris Green |
last post by:
Good day,
I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as...
|
by: bonono |
last post by:
Hi,
Suppose my class definition is like this :
class A:
name = "A"
@classmethod
def foo(cls):
cls.__super.foo()
|
by: Andrew Jaffe |
last post by:
Hi,
I have a class with various class-level variables which are used to
store global state information for all instances of a class. These are
set by a classmethod as in the following (in...
|
by: Christoph Zwerschke |
last post by:
Usually, you initialize class variables like that:
class A:
sum = 45
But what is the proper way to initialize class variables if they are the
result of some computation or processing as in...
| |
by: ddtl |
last post by:
Hello everybody.
Consider the following code:
class A(object):
def met(self):
print 'A.met'
class B(A):
def met(self):
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it....
|
by: Steven D'Aprano |
last post by:
Here's a simple class-factory function that returns a sub-class of the
old-style class it is passed.
def verbosify_oclass(klass):
"""Returns a verbose sub-class of old-style klass."""
class...
|
by: Gordon |
last post by:
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:
...
|
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...
|
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,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |