473,288 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Weird exception in my, um, exception class constructor

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
6 1152
"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
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
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
"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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? ...
3
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...
3
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...
8
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.