Ternary operator and tuple unpacking -- What am I missing ?

imageguy imageguy1206 at gmail.com
Tue Jan 13 06:02:08 CET 2009


Using py2.5.4 and entering the following lines in IDLE, I don't really
understand why I get the result shown in line 8.

Note the difference between lines 7 and 10 is that 'else' clause
result enclosed in brackets, however, in line 2, both the 'c,d'
variables are assign correctly without the brackets being required.

Any chance someone could enlighten me on the rules for tuple unpacking
as this seems inconsistent.

1) >>> n = None
2) >>> c,d = n if n is not None else 0,0
3) >>> print c,d, type(c), type(d)
4) 0 0 <type 'int'> <type 'int'>
5) >>> n = 22,11
6) >>> print n, type(n)
(22, 11) <type 'tuple'>
7) >>> c,d = n if n is not None else 0,0
8) >>> print c,d
9) (22, 11) 0
10)  >>> c,d = n if n is not None else (0,0)
11) >>> print c,d
12) 22 11





More information about the Python-list mailing list