Deriving from Exception
pixie888 at hotmail.com
pixie888 at hotmail.com
Wed Jul 10 07:20:42 EDT 2002
On Wed, 10 Jul 2002 09:51:14 GMT, "Fredrik Lundh"
<fredrik at pythonware.com> wrote:
>"pixie888 at hotmail.com" wrote:
>
>> 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?
>
>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.
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>
>
>
More information about the Python-list
mailing list