weird behaviour of "0 in [] is False"
John Roth
newsgroups at jhrothjr.com
Tue Nov 30 15:30:16 EST 2004
"Paul Robson" <autismuk at autismuk.muralichucks.freeserve.co.uk> wrote in
message news:coi1hc$9go$1 at news8.svr.pol.co.uk...
> Sylvain Thenault wrote:
>
>>>>> l = []
>>>>> 0 in (l is False)
>
> (l is False) is not a tuple or list, it's a boolean value.
>
>> 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 becuase l is empty, so it's False is False which is true,
> (except in Intercal probably and Visual C++)
>
>>>>> 0 in l is False
>> False
>
> l is False is False because l is not the value false though it has a false
> value (err.....)
>
> Okay.
>
> l != False because it's not the displayed value false
>
> but if not l would evaluated to true because [] is a false equivalent.
>
> 0 in False .... okay.... this should be an error ..... something to do
> with
> the equivalence confusion of what False is ?
It's not an error. As one of the first responders said, check
the language definition. That defines both 'in' and 'is'
as equality operators, and defines exactly what a chain
of equality operators means.
In this case, it means:
(0 in l) and (l is False)
The and short circuits, giving the result of False without
ever doing the final comparison.
Granted, that's not exactly obvious...
John Roth
>
>
>
More information about the Python-list
mailing list