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 6 1155
"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
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
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
"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...
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pierre Rouleau |
last post by:
The std::exception class defined in the Standard C++ <exception> header
specifies that the constructors could throw any exception becuase they
do not have a throw() specification.
Why is that? ...
|
by: Versteijn |
last post by:
Hello all,
I have a custom Exception inherited class with the following
constructor. I added the IIf call because the Parameters() value might
be null. But somehow I keep getting an...
|
by: matko |
last post by:
This is a long one, so I'll summarize:
1. What are your opinions on raising an exception within the
constructor of a (custom) exception?
2. How do -you- validate arguments in your own...
|
by: nickyeng |
last post by:
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run.
My simple aim is to display the terrain.txt file into the terrain array, and then...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |