keyword 'in' not returning a bool?

Larry Bates larry.bates at websafe.com
Fri Feb 8 12:15:58 EST 2008


c james wrote:
> Try this
> 
>>>> sample = {'t':True, 'f':False}
>>>> 't' in sample
> True
>>>> type('t' in sample)
> <type 'bool'>
>>>> 't' in sample == True
> False
> 
> Why is this?  Now try
>>>> bool('t' in sample) == True
> True
> 
> Can someone explain what is going on?
> 

Precedence.  In:

't' in sample == True

sample == True is evaluated first
then 't' in that which is false

you should write

('t' in sample) == True

but that is unnecessary because

t' in sample

is easier to read and to code and less prone to this problem.

-Larry



More information about the Python-list mailing list