Weird exception in my, um, exception class constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joel Koltner

    Weird exception in my, um, exception class constructor

    I have a generic (do nothing) exception class that's coded like this:

    class MyError(excepti ons.Exception):
    def __init__(self,a rgs=None):
    self.args = args

    When I attempt to raise this exception via 'raise MyError' I get an exception
    within the MyError constructor __init__ as follows:

    Traceback (most recent call last):
    [lines deleted]
    File "c:\Documen ts and Settings\Joel.K olstad\My Documents\Pytho n\
    MyStuff.py", line 7, in __init__
    self.args = args
    TypeError: 'NoneType' object is not iterable

    Ummm... why should 'args' have to be iterable anyway? I don't understand
    what's going on here? Could someone help me with this?

    I've found empirically that I can write:

    if args self.args = args

    ....and everything *appears* to work OK, but I'd really like to know what the
    core problem is.

    Thank you,
    ---Joel



  • Arnaud Delobelle

    #2
    Re: Weird exception in my, um, exception class constructor

    "Joel Koltner" <zapwireDASHgro ups@yahoo.comwr ites:
    I have a generic (do nothing) exception class that's coded like this:
    >
    class MyError(excepti ons.Exception):
    def __init__(self,a rgs=None):
    self.args = args
    >
    When I attempt to raise this exception via 'raise MyError' I get an
    exception within the MyError constructor __init__ as follows:
    >
    Traceback (most recent call last):
    [lines deleted]
    File "c:\Documen ts and Settings\Joel.K olstad\My Documents\Pytho n\
    MyStuff.py", line 7, in __init__
    self.args = args
    TypeError: 'NoneType' object is not iterable
    That's because the class 'Exception' defines a descriptor 'args' which
    has to be a sequence. Just call 'args' something else, and it will
    work. E.g.
    >>class MyError(Excepti on):
    .... def __init__(self, args=None):
    .... self.myargs = args
    ....
    >>MyError()
    MyError()


    OTOH you could just take advantage of Exception.args:
    >>class MyError(Excepti on): pass
    ....
    >>MyError()
    MyError()
    >>MyError(1, 2, 3)
    MyError(1, 2, 3)

    (Details in the documentation are a bit scant, but see
    http://docs.python.org/lib/module-exceptions.html)

    HTH

    --
    Arnaud

    Comment

    • Paul Hankin

      #3
      Re: Weird exception in my, um, exception class constructor

      On May 27, 9:21 pm, "Joel Koltner" <zapwireDASHgro ...@yahoo.com>
      wrote:
      I have a generic (do nothing) exception class that's coded like this:
      >
      class MyError(excepti ons.Exception):
          def __init__(self,a rgs=None):
              self.args = args
      >
      When I attempt to raise this exception via 'raise MyError' I get an exception
      within the MyError constructor __init__ as follows:
      >
      Traceback (most recent call last):
      [lines deleted]
        File "c:\Documen ts and Settings\Joel.K olstad\My Documents\Pytho n\
      MyStuff.py", line 7, in __init__
          self.args = args
      TypeError: 'NoneType' object is not iterable
      >
      Ummm... why should 'args' have to be iterable anyway?  I don't understand
      what's going on here?  Could someone help me with this?
      Did you actually write self,args = args?

      --
      Paul Hankin

      Comment

      • Joel Koltner

        #4
        Re: Weird exception in my, um, exception class constructor

        Hi Arnaud,

        "Arnaud Delobelle" <arnodel@google mail.comwrote in message
        news:m2wslf4haq .fsf@googlemail .com...
        That's because the class 'Exception' defines a descriptor 'args' which
        has to be a sequence.
        Ah, thanks. I was following the example in Beazley's book and should have dug
        into the actual documentation a bit more.
        OTOH you could just take advantage of Exception.args:
        >>>class MyError(Excepti on): pass
        Sounds good to me. I take it that, if I don't inherit from Exception, various
        expected behaviors will break? (This is what Beazley suggests...)

        ---Joel


        Comment

        • Joel Koltner

          #5
          Re: Weird exception in my, um, exception class constructor

          "Paul Hankin" <paul.hankin@gm ail.comwrote in message
          news:0ea08237-c9f9-441b-a744-7410114b5ae9@t5 4g2000hsg.googl egroups.com...
          "Did you actually write self,args = args?"

          (looks at source code)

          $#@#%#$ Why, yes, yes I did! Thanks for catching that...


          Comment

          • Terry Reedy

            #6
            Re: Weird exception in my, um, exception class constructor


            "Joel Koltner" <zapwireDASHgro ups@yahoo.comwr ote in message
            news:Kq__j.1770 27$ng7.15947@en-nntp-05.dc1.easynews .com...
            | Sounds good to me. I take it that, if I don't inherit from Exception,
            various
            | expected behaviors will break? (This is what Beazley suggests...)

            All builtin exceptions have been in the builtin namespace for a while. The
            duplicate exceptions module is gone in 3.0, In 3.0, if not before, all
            exceptions must subclass from one of the builtin exceptions. Exception is
            the usual choice.

            tjr





            Comment

            • Arnaud Delobelle

              #7
              Re: Weird exception in my, um, exception class constructor

              "Joel Koltner" <zapwireDASHgro ups@yahoo.comwr ites:
              "Paul Hankin" <paul.hankin@gm ail.comwrote in message
              news:0ea08237-c9f9-441b-a744-7410114b5ae9@t5 4g2000hsg.googl egroups.com...
              "Did you actually write self,args = args?"
              >
              (looks at source code)
              >
              $#@#%#$ Why, yes, yes I did! Thanks for catching that...
              This is odd, because you should get this error even if writing
              self.args=args, witness this copy-paste from the interactive prompt:
              >>class MyError(Excepti on):
              .... def __init__(self, args=None):
              .... self.args = args
              ....
              >>MyError()
              Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
              File "<stdin>", line 3, in __init__
              TypeError: 'NoneType' object is not iterable

              --
              Arnaud

              Comment

              Working...