Tuple Unpacking in raise

James Stroud jstroud at mbi.ucla.edu
Mon Jun 20 21:46:37 EDT 2005


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.__init__(self, "Error with %s-%s" % atup)
> ...
> py> raise E, ((1, 2),)
> Traceback (most recent call last):
>    File "<interactive 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/



More information about the Python-list mailing list