Tuple Unpacking in raise

Discussion in 'Python' started by James Stroud, Jun 21, 2005.

  1. James Stroud

    James Stroud Guest

    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.__init__(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/
     
    James Stroud, Jun 21, 2005
    #1
    1. Advertising

  2. 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.__init__(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.__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
     
    Steven Bethard, Jun 21, 2005
    #2
    1. Advertising

  3. On 6/21/05, Steven Bethard <> 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.__init__(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
     
    Konstantin Veretennicov, Jun 21, 2005
    #3
  4. James Stroud

    James Stroud Guest

    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/
     
    James Stroud, Jun 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul McGuire
    Replies:
    8
    Views:
    1,187
    Mark Wooding
    Nov 24, 2004
  2. harold
    Replies:
    11
    Views:
    571
    John Machin
    Apr 23, 2006
  3. Patrick Toomey

    Can my own objects support tuple unpacking?

    Patrick Toomey, Mar 27, 2008, in forum: Python
    Replies:
    2
    Views:
    313
  4. Martin Geisler

    Tuple parameter unpacking in 3.x

    Martin Geisler, Oct 2, 2008, in forum: Python
    Replies:
    20
    Views:
    773
    Aaron \Castironpi\ Brady
    Oct 11, 2008
  5. imageguy
    Replies:
    7
    Views:
    294
    John Machin
    Jan 13, 2009
Loading...

Share This Page