[Tutor] Calling super in __init__

wcyee wcyeee at gmail.com
Mon May 19 09:27:06 CEST 2008


Hi - I wrote a custom exception class as follows:

class CustomError(Exception):
    def __init__(self, msg):
        super(CustomError, self).__init__(self, msg)

But this doesn't work as expected:

try:
    raise CustomError('something bad')
except CustomError, err:
    print err.message

err.message is a blank string. I fixed this eventually by rewriting the call
to the super constructor:

class CustomError(Exception):
    def __init__(self, msg):
        Exception.__init__(self, msg)

Now everything works, but I'm not sure I understand why
super(...).__init__(...) behaves differently from Exception.__init__(...).

According to the documentation, "super() only works with new-style classes".
Is the problem that Exception is an "old style" class (I'm still learning
what this means)? If so, how can I tell when I'm up against an "old style"
class when using python? Why does super() fail silently, if this is the
case?

Thanks in advance for any pointers or help in understanding this!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080519/c3fc2747/attachment.htm>


More information about the Tutor mailing list