Pythoner
Pythoner

Reputation: 5585

What's the use of python syntax raise A, L?

If the following code is called where L is a list, how is L used?

raise A, L

Upvotes: 0

Views: 46

Answers (1)

Amadan
Amadan

Reputation: 198354

Note that this syntax is only available in Python 2. Python 3 has lib2to3/fixes/fix_raise.py

in its 2-to-3 translation system, which starts with the following docstring, giving you a hint about things the documentation stays silent on (AFAIK):

Fixer for 'raise E, V, T'

raise         -> raise
raise E       -> raise E
raise E, V    -> raise E(V)
raise E, V, T -> raise E(V).with_traceback(T)
raise E, None, T -> raise E.with_traceback(T)

raise (((E, E'), E''), E'''), V -> raise E(V)
raise "foo", V, T               -> warns about string exceptions


CAVEATS:
1) "raise E, V" will be incorrectly translated if V is an exception
   instance. The correct Python 3 idiom is

        raise E from V

   but since we can't detect instance-hood by syntax alone and since
   any client code would have to be changed as well, we don't automate
   this.

What it boils down to is that raise FooException, [1, 2] is equivalent to raise FooException([1, 2]); but you should always use the latter.

Upvotes: 1

Related Questions