473,695 Members | 1,967 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 1165
"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
3493
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
1093
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
3403
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
2208
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
8977
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8841
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
8822
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
7658
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...
1
6488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4339
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...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
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
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.