keyword 'in' not returning a bool?

Eyal Lotem eyal.lotem at gmail.com
Sat Feb 9 18:12:03 CET 2008


Arnaud Delobelle wrote:

> On Feb 8, 5:20 pm, "Reedick, Andrew" <jr9... at ATT.COM> wrote:
>> > -----Original Message-----
>> > From: python-list-bounces+jr9445=att.... at python.org [mailto:python-
>> > list-bounces+jr9445=att.... at python.org] On Behalf Of c james
>> > Sent: Friday, February 08, 2008 12:10 PM
>> > To: python-l... at python.org
>> > Subject: keyword 'in' not returning a bool?
>>
>> > 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?
>>
>> >>> ('t' in sample) == True
>>
>> True
>>
>> It's operator precedence. 'in' has lower precedence than '=='. Therefore
>> 't' in sample == True
>> evaluates as
>> 't' in (sample == True)
>>
>> The real question is why does
>> 't' in (sample == True)
>> cause an error:
>> TypeError: argument of type 'bool' is not iterable
>> while
>> 't' in sample == True
>> does not?
> 
> That should have told you precedence is not the reason!

I would guess that it is the same reason that:
1 <= (2 < 3)
is different than:
1 <= 2 < 3

Condition-chaining, I think its called, though I might be wrong.

> 
> --
> Arnaud




More information about the Python-list mailing list