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

Paul Rubin http
Tue Jan 13 17:01:25 CET 2009

imageguy <imageguy1206 at gmail.com> writes:
> 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.

    c,d = n if n is not None else 0,0

parses as:
    c,d = (n if n is not None else 0), 0

In the case where n is None, c and d are both set to 0.

In the case where n is a tuple, c is set to the tuple and d is set to 0.

Does that help?

More information about the Python-list mailing list