By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,995 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,995 IT Pros & Developers. It's quick & easy.

Weird exception in my, um, exception class constructor

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

class MyError(exceptions.Exception):
def __init__(self,args=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:\Documents and Settings\Joel.Kolstad\My Documents\Python\
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

Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Joel Koltner" <za***************@yahoo.comwrites:
I have a generic (do nothing) exception class that's coded like this:

class MyError(exceptions.Exception):
def __init__(self,args=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:\Documents and Settings\Joel.Kolstad\My Documents\Python\
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(Exception):
.... def __init__(self, args=None):
.... self.myargs = args
....
>>MyError()
MyError()
OTOH you could just take advantage of Exception.args:
>>class MyError(Exception): 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
Jun 27 '08 #2

P: n/a
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(exceptions.Exception):
* * def __init__(self,args=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:\Documents and Settings\Joel.Kolstad\My Documents\Python\
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
Jun 27 '08 #3

P: n/a
Hi Arnaud,

"Arnaud Delobelle" <ar*****@googlemail.comwrote in message
news:m2************@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(Exception): 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
Jun 27 '08 #4

P: n/a
"Paul Hankin" <pa*********@gmail.comwrote in message
news:0e**********************************@t54g2000 hsg.googlegroups.com...
"Did you actually write self,args = args?"

(looks at source code)

$#@#%#$ Why, yes, yes I did! Thanks for catching that...
Jun 27 '08 #5

P: n/a

"Joel Koltner" <za***************@yahoo.comwrote in message
news:Kq********************@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

Jun 27 '08 #6

P: n/a
"Joel Koltner" <za***************@yahoo.comwrites:
"Paul Hankin" <pa*********@gmail.comwrote in message
news:0e**********************************@t54g2000 hsg.googlegroups.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(Exception):
.... 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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.