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

I

imageguy

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
 
I

imageguy

understand why I get the result shown in line 8.

7) >>> c,d = n if n is not None else 0,0
8) >>> print c,d
9) (22, 11) 0

OOPS sorry that should be line 9.

g.
 
M

Miles

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.

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'>

The ternary expression has higher precedence than the comma, so the
actual effect of line 2 (and 8) is:

Or written more explicitly:

So the only correct way to write the expression, for the result you
want, is to use your line 10:
10) >>> c,d = n if n is not None else (0,0)

But if you're struggling with the precedence issues, I'd recommend
ditching ternary expressions altogether and using full conditional
blocks.

-Miles
 
S

Steve Holden

Miles said:
The ternary expression has higher precedence than the comma, so the
actual effect of line 2 (and 8) is:


Or written more explicitly:


So the only correct way to write the expression, for the result you
want, is to use your line 10:


But if you're struggling with the precedence issues, I'd recommend
ditching ternary expressions altogether and using full conditional
blocks.
Yet another great example of why Guido was right to resist putting
conditional expressions into Python for so long (and wrong to succumb to
the demand).

regards
Steve
 
T

Tim Roberts

imageguy said:
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.

It's not the tuple unpacking that's burning you. It's simple operator
precedence.
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

As line 10 makes clear, line 7 is interpreted thus:

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

"c" gets bound to the result of the ternary (the tuple (22,11)), and "d"
gets bound to 0.
 
P

Paul Rubin

imageguy said:
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?
 
I

imageguy

The ternary expression has higher precedence than the comma, so the
actual effect of line 2 (and 8) is:


Or written more explicitly:


So the only correct way to write the expression, for the result you
want, is to use your line 10:


But if you're struggling with the precedence issues, I'd recommend
ditching ternary expressions altogether and using full conditional
blocks.

-Miles

Thanks.
Hadn't thought through the operator precedence and the affect of the
comma.
This was the first time I tried to use with tuples, so will be more
careful next time
or stick to control blocks.

g.
 
J

John Machin

Yet another great example of why Guido was right to resist putting
conditional expressions into Python for so long (and wrong to succumb to
the demand).

"""I thought I said "Nobody mention the war!" """

IMO this is just an example of why (1) in general people who are
unsure of operator precedence should use parentheses and (2) in
particular it's not a good idea to try to write tuples without
parentheses in any but the simpler cases like a, b = b, a
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top