weird behaviour of "0 in [] is False"

Duncan Booth duncan.booth at invalid.invalid
Tue Nov 30 08:24:42 EST 2004


Sylvain Thenault wrote:

> Hi there !
> 
> Can someone explain me the following behaviour ?
> 
>>>> l = []
>>>> 0 in (l is False)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: iterable argument required
>>>> (0 in l) is False
> True
>>>> 0 in l is False
> False
> 
> 
> This is really obscur to me...
> 

>From the language reference (5.9 Comparisons):

> comparison  ::=  or_expr ( comp_operator or_expr )* 
> comp_operator  ::=  "<" | ">" | "==" | ">=" | "<=" | "<>" | "!=" 
>     | "is" ["not"] | ["not"] "in" 
>
> ...snip...
>
> Formally, if a, b, c, ..., y, z are expressions and opa, opb, ..., opy
> are comparison operators, then a opa b opb c ...y opy z is equivalent
> to a opa b and b opb c and ... y opy z, except that each expression is
> evaluated at most once. 

In other words '0 in l is False' is equivalent to '0 in l and l is False'.




More information about the Python-list mailing list