Exception.__init__ needed in derived class?
george young
gry at ll.mit.edu
Wed Apr 23 14:26:50 EDT 2003
On Wed, 23 Apr 2003 14:07:54 -0400
george young <gry at ll.mit.edu> threw this fish to the penguins:
> [python-2.3a1]
> Does class Exception have a constructor __init__ that needs
> to be called? I couldn't find this in the docs. I see many
> uses of inheriting from Exception without invoking
> Exception.__init__(self), but nobody ever *said* that this
> was safe to leave out...
>
> Pychecker complains about this, which is what brought it to
> mind. I can certainly invoke it just to make pychecker
> happy, but I wondered...
To elaborate, I often want to define a __init__ for my
Exception subclass because I want to give it named data, e.g.:
class BadRun(Exception):
def __init__(self, name, id):
self.name=name
self.id=id
...
raise BadRun(newname, current_id)
...
try:
self.run = Run('implant_test', self.step_id)
except BadRun, x:
print 'Run %s, id=%s can not be created',x.name, x.id
Would it be better to do:
class BadRun(Exception): pass
...
ex = BadRun()
ex.name='implant_test'; ex.id=self.step_id
raise ex
More information about the Python-list
mailing list