Exceptions, assigning a tuple

Mel Wilson mwilson at the-wire.com
Fri Nov 21 16:58:52 EST 2003


In article <3FBDBA3E.23D54A7C at alcyone.com>,
Erik Max Francis <max at alcyone.com> wrote:
>Derek Fountain wrote:
>
>> What I don't understand is the assignment of a tuple in the except
>> clause.
>> [ ... ]
>This is just using tuple unpacking in assignment:
>
>>>> tup = 1, 2, 3
>>>> x, y, z = tup
>>>> x
>1
>>>> y
>2
>>>> z
>3
>
>This syntax can be used anywhere an lvalue is allowed, which might
>include the "variable" of a for statement:
> [ ... ]
>and the "variable" in a try/except clause is just another example of
>this.  [ ... ]

   The use of tuple unpacking that surprised me most is in
function arguments:


Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f ( (a,b) ):
...     print a, b
...
>>> x = (1,2)
>>> f(x)
1 2
>>>


   It almost looks like Prolog unification (but isn't
really.)

        Regards.        Mel.




More information about the Python-list mailing list