weird behaviour of "0 in [] is False"

Diez B. Roggisch deetsNOSPAM at web.de
Tue Nov 30 14:18:30 CET 2004


>>>> l = []
>>>> 0 in (l is False)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: iterable argument required

that should be clear - 0 in False can't work.

>>>> (0 in l) is False
> True
>>>> 0 in l is False
> False

It seems to stem from a behaviour python exhibits in expressions like this:

3 > 2  > 1

This yields True, while

(3 > 2 ) > 1

yields false. Chaining of operators gets translated like this:

3 > 2 and 2 > 1

Look in section 5.9 of the language reference.

Then your expression gets translated to:

0 in l and l is False

which yields False of course.


-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list