Pychecker warns about Exception example from Python tutorial

P

Pekka Niiranen

Hi there,

why does pychecker v0.8.14 gives this warning:

koe2.py:2: Base class (exceptions.Exception) __init__() not called

when run against this example from Python Tutorial:

------------- code starts -------
class MyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

try:
raise MyError(2*2)
except MyError, e:
print 'My exception occurred, value:', e.value

------------- code starts -------

What does this warning mean and how can I disable it?

-pekka-
 
V

Ville Vainio

Pekka> Hi there,
Pekka> why does pychecker v0.8.14 gives this warning:

Pekka> koe2.py:2: Base class (exceptions.Exception) __init__() not called




Pekka> What does this warning mean and how can I disable it?

You had __init__, and you didn't call the init of the superclass. This
might leave the superclass in uninitialized stage. Your own exception
class is over-engineered, this works the way you want:

--------------------------
In [11]: class MyEx(Exception): pass
....:

In [12]: raise MyEx("blah")
---------------------------------------------------------------------------
MyEx Traceback (most recent call last)

/home/ville/<console>

MyEx: blah
 
J

Josiah Carlson

You can fix your pychecker error by adding the non-quoted line below...

------------- code starts -------
class MyError(Exception):
def __init__(self, value):
Exception.__init__(self)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top