class 'Exception', unable to use 'super' to call superclass initializer

C

chriss

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
 
R

Robert Kern

chriss said:
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
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
P

Peter Hansen

chriss said:
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
 
C

chriss

Peter said:
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top