473,796 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jun 27 '08 #1
6 1173
"Joel Koltner" <za************ ***@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
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(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
Jun 27 '08 #3
Hi Arnaud,

"Arnaud Delobelle" <ar*****@google mail.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(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
Jun 27 '08 #4
"Paul Hankin" <pa*********@gm ail.comwrote in message
news:0e******** *************** ***********@t54 g2000hsg.google groups.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.comwr ote 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.comwr ites:
"Paul Hankin" <pa*********@gm ail.comwrote in message
news:0e******** *************** ***********@t54 g2000hsg.google groups.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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3505
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? Is this because there could be an exception thrown when the code creates a std::exception? I would assume that is not the case. However, if I want to create a new exception class, derived from std::exception (say MyException) then how can I...
3
1099
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 ArgumentNullException on String.Format: 'Value cannot be null. Parameter name: args'. If I replace this FalsePart of IIf with a random string like "hello world", nothing is wrong, the correct part (TruePart) is used and I get no exception. It looks like the...
3
3407
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 exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is to be supplied to the exception constructor, the default constructor should
8
2213
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 display terrain into screen. I can't display the whitespace characters from terrain.txt file to screen. and It display "weird"( the red color part). http://i19.photobucket.com/albums/b171/NickyEng/display.jpg I tried searching one line by...
0
9683
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.