Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > class 'Exception', unable to use 'super' to call superclass initializer

Reply
Thread Tools

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

 
 
chriss
Guest
Posts: n/a
 
      09-10-2005
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



 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      09-10-2005
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


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

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      09-10-2005
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
 
Reply With Quote
 
chriss
Guest
Posts: n/a
 
      09-10-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if another object is my superclass from a function in aneven higher superclass? bart van deenen C++ 6 03-03-2009 02:44 PM
finding the parent class (not superclass) of the currently executingmethod derived from a Borg class seanacais Python 1 09-09-2008 08:35 AM
Class.getMethod in class's static initializer block chucky Java 14 08-02-2007 12:22 PM
calling superclass __init__ when superclass is object Evan Klitzke Python 0 08-02-2007 05:15 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57