keyongtech


  keyongtech > python > 01/2009

 #1  
01-13-09, 05:02 AM
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
 #2  
01-13-09, 05:08 AM
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.
 #3  
01-13-09, 06:01 AM
Miles
On Tue, Jan 13, 2009 at 12:02 AM, imageguy <imageguy1206> wrote:
> 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:

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


Or written more explicitly:

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


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
 #4  
01-13-09, 06:36 AM
Steve Holden
Miles wrote:
> On Tue, Jan 13, 2009 at 12:02 AM, imageguy <imageguy1206> wrote:
>
> 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
 #5  
01-13-09, 07:29 AM
Tim Roberts
imageguy <imageguy1206> wrote:

>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.
 #6  
01-13-09, 03:47 PM
Scott David Daniels
imageguy wrote:

> 1) >>> n = None
> 2) >>> c,d = n if n is not None else 0,0

....


This is more easily expressed as:
c, d = n or (0, 0)
 #7  
01-13-09, 04:01 PM
Paul Rubin
imageguy <imageguy1206> 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?
 #8  
01-13-09, 08:43 PM
imageguy
On Jan 13, 1:01 am, Miles <semantic> wrote:
[..]
>
> 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.
 #9  
01-13-09, 09:58 PM
John Machin
On Jan 13, 5:36 pm, Steve Holden <st> wrote:
> Miles wrote:
>>
>>
>>

>
> 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
Similar Threads
Cython + tuple unpacking

Hello, The following code will crash with a segfault when compiled using cython (v0.11) def func(): for (a, b) ,c ,d in zip(zip(range(3), range(3)), range(3),...

Tuple parameter unpacking in 3.x

-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9...

problems when unpacking tuple ...

Dear all, Maybe I stared on the monitor for too long, because I cannot find the bug ... My script "transition_filter.py" starts with the following lines: import sys for...

Tuple Unpacking in raise

Hello All, Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some subtle logic? Why does print not work the same way as raise? Both are statements....

Idiom for default values when unpacking a tuple

I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList =...


All times are GMT. The time now is 06:08 AM. | Privacy Policy