Pychecker warns about Exception example from Python tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pekka Niiranen

    Pychecker warns about Exception example from Python tutorial

    Hi there,

    why does pychecker v0.8.14 gives this warning:

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

    when run against this example from Python Tutorial:

    ------------- code starts -------
    class MyError(Excepti on):
    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-
  • Ville Vainio

    #2
    Re: Pychecker warns about Exception example from Python tutorial

    >>>>> "Pekka" == Pekka Niiranen <pekka.niiranen @wlanmail.com> writes:

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

    Pekka> koe2.py:2: Base class (exceptions.Exc eption) __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
    ---------------------------------

    --
    Ville Vainio http://tinyurl.com/2prnb

    Comment

    • Josiah Carlson

      #3
      Re: Pychecker warns about Exception example from Python tutorial


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

      [color=blue]
      > ------------- code starts -------
      > class MyError(Excepti on):
      > def __init__(self, value):[/color]
      Exception.__ini t__(self)[color=blue]
      > 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[/color]

      Comment

      Working...