Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deriving from Exception

0 views
Skip to first unread message

pixi...@hotmail.com

unread,
Jul 10, 2002, 4:50:20 AM7/10/02
to
Hi,

I have some classes which derive from Exception, however in the
__init__ function I do not call the __init__ function of Exception
itself, allthough I think I should. The reason I don't do it is
because I see that in all the tutorials about Python the tutors
themselves don't do it either.

Can anybody tell me why?

Thanks,

Henk

Fredrik Lundh

unread,
Jul 10, 2002, 5:51:14 AM7/10/02
to
"pixi...@hotmail.com" wrote:

The Exception baseclass provides an __init__ method which
takes all arguments and puts them in an args attribute.

It also provides default __str__ and __getitem__ methods,
so you can print the exception, and use the v[x] notation
to access the members.

In Python, the class would look something like this:

class Exception:

def __init__(self, *args):
self.args = args

def __str__(self):
if not self.args:
return ''
elif len(self.args) == 1:
return str(self.args[0])
else:
return str(self.args)

def __getitem__(self, i):
return self.args[i]

If this behaviour is fine for your exception subclass, there's
no need to override anything.

</F>


pixi...@hotmail.com

unread,
Jul 10, 2002, 7:20:42 AM7/10/02
to

Example

class CMyException(Exception):
def __init__(self,ID):
self.ID = ID

I do NOT call the __init__ function of Exception in the __init__
function of CMyException: how does that relate to the explanation you
gave above? Will those args (what are they anyway, is args a reserved
word?) be copied then? I tought that *not* calling the __init__ of
Exception in the __init__ of CMyException resulted in not running the
code provided in that base __init__ function. Did I miss something
here?


>
></F>
>
>

Hans Nowak

unread,
Jul 11, 2002, 12:05:01 AM7/11/02
to
[/F]

>>If this behaviour is fine for your exception subclass, there's
>>no need to override anything.

[pixie888]


> class CMyException(Exception):
> def __init__(self,ID):
> self.ID = ID
>
> I do NOT call the __init__ function of Exception in the __init__
> function of CMyException: how does that relate to the explanation you
> gave above? Will those args (what are they anyway, is args a reserved
> word?) be copied then? I tought that *not* calling the __init__ of
> Exception in the __init__ of CMyException resulted in not running the
> code provided in that base __init__ function. Did I miss something
> here?

Sort of: the point is that in normal cases, you don't have to write your own
__init__ here. For example:

class CMyException(Exception): pass

try:
raise CMyException("42")
except CMyException, e:
print e.args

# prints ('42',)

What you pass to the exception can be retrieved using the args attribute. Of
course, your situation is a bit different, you store the ID as an attribute of
the exception object... is there a special reason why you want to do that?

--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/

0 new messages