473,569 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tuple Unpacking in raise

Hello All,

Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some
subtle logic? Why does print not work the same way as raise? Both are
statements. Why does raise need to be so special?

py> sometup = 1,2
py> print sometup
(1, 2)
py> print 1,2,3, sometup
1 2 3 (1, 2)
py> class MyErr(Exception ):
.... def __init__(self, atup):
.... Exception.__ini t__(self, "Error with %s-%s" % atup)
....
py> raise MyErr, sometup
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __init__() takes exactly 2 arguments (3 given)
py> e = MyErr(sometup)
py> print e
Error with 1-2

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #1
3 1826
James Stroud wrote:
Hello All,

Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some
subtle logic? Why does print not work the same way as raise? Both are
statements. Why does raise need to be so special?

py> sometup = 1,2
py> print sometup
(1, 2)
py> print 1,2,3, sometup
1 2 3 (1, 2)
py> class MyErr(Exception ):
... def __init__(self, atup):
... Exception.__ini t__(self, "Error with %s-%s" % atup)
...
py> raise MyErr, sometup
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __init__() takes exactly 2 arguments (3 given)
py> e = MyErr(sometup)
py> print e
Error with 1-2


Well, it's not a bug, because that's what the documentation says it'll do:

"The second object is used to determine the exception value: If it is an
instance of the class, the instance becomes the exception value. If the
second object is a tuple, it is used as the argument list for the class
constructor; if it is None, an empty argument list is used, and any
other object is treated as a single argument to the constructor."[1]

In the example above (as well as almost all code), there's no need to
use the two argument version of raise. You can simply write:

raise MyErr(sometup)

If you need the three argument version of raise, I believe you can still
write it as:

raise MyErr(sometup), None, tb

Note that Guido has mentioned a few times that in Python 3.0, he wants
tracebacks to be attributes of the Exception objects so that all raise
statements are like the one argument version.

STeVe

P.S. If you insist on using the two argument version of raise, you can
do it like this:

py> class E(Exception):
.... def __init__(self, atup):
.... Exception.__ini t__(self, "Error with %s-%s" % atup)
....
py> raise E, ((1, 2),)
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
E: Error with 1-2

But that seems a lot less elegant than simply using the one argument
version.

[1] http://docs.python.org/ref/raise.html
Jul 19 '05 #2
On 6/21/05, Steven Bethard <st************ @gmail.com> wrote:
James Stroud wrote:
P.S. If you insist on using the two argument version of raise, you can
do it like this:

py> class E(Exception):
... def __init__(self, atup):
... Exception.__ini t__(self, "Error with %s-%s" % atup)

But that seems a lot less elegant than simply using the one argument
version.


Another workaround would be to use __init__(self, *atup), but raising
an explicitly constructed exception is preferable (IMO).

- kv
Jul 19 '05 #3
Thank you Steven and Konstantin, that clears things up.

Sometimes I forget how incomplete my Python Essential Reference is.

James

On Monday 20 June 2005 05:40 pm, Steven Bethard wrote:
Well, it's not a bug, because that's what the documentation says it'll do:

"The second object is used to determine the exception value: If it is an
instance of the class, the instance becomes the exception value. If the
second object is a tuple, it is used as the argument list for the class
constructor; if it is None, an empty argument list is used, and any
other object is treated as a single argument to the constructor."[1]

In the example above (as well as almost all code), there's no need to
use the two argument version of raise. You can simply write:

raise MyErr(sometup)

If you need the three argument version of raise, I believe you can still
write it as:

raise MyErr(sometup), None, tb

Note that Guido has mentioned a few times that in Python 3.0, he wants
tracebacks to be attributes of the Exception objects so that all raise
statements are like the one argument version.

STeVe

P.S. If you insist on using the two argument version of raise, you can
do it like this:

py> class E(Exception):
... def __init__(self, atup):
... Exception.__ini t__(self, "Error with %s-%s" % atup)
...
py> raise E, ((1, 2),)
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
E: Error with 1-2

But that seems a lot less elegant than simply using the one argument
version.

[1] http://docs.python.org/ref/raise.html


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #4

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

Similar topics

50
3658
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this, I'm just curious why it is neccesary.. Thanks,
8
3618
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something with a,b, and c Now I would like to add a special fourth config value to "T": ("T",1,5,4,0.005),
12
1590
by: Kay Schluehr | last post by:
Hi all, thanks for Your attention ! I think my proposal was more in mind of Rons modified exec than Pythons lambda. When George proposed his unpacking behavoir for list-comps as a pack of suggar:
16
22999
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
11
1621
by: harold | last post by:
Dear all, Maybe I stared on the monitor for too long, because I cannot find the bug ... My script "transition_filter.py" starts with the following lines: import sys for line in sys.stdin : try :
43
3342
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In this case, just zeros (though I just to prove whatever ends up working, having a counting generator would be nice). The target syntax would be...
25
4080
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to the function. For example, I have all the arguments of a function in a tuple a=(1,2,3). Then I want to pass each item in the tuple to a function f...
21
7234
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP SIGNATURE-----
0
7614
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...
0
8125
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...
1
7676
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...
0
5219
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...
0
3653
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...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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...

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.