isinstance(False, int)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Mar 5 18:09:58 EST 2010


On Fri, 05 Mar 2010 15:58:01 -0500, Jack Diederich wrote:

>>> So, the pythonic way to check for True/False should be:
>>>
>>>>>> 1 is True
>>> False
>>
>> Why do you need to check for True/False?
>>
>>
> You should never check for "is" False/True but always check for
> equality.  The reason is that many types support the equality (__eq__)
> and boolen (__bool__ in 3x) protocols.  If you check equality these will
> be invoked, if you check identity ("is") they won't.

Never say never.

If you specifically want to test for True or False themselves, accepting 
no substitutes, then using "is" is the obvious way, and using "==" is 
clearly and obviously wrong because it does accept substitutes:

>>> 1.0 == True
True
>>> decimal.Decimal(0, 1) == False
True



-- 
Steven



More information about the Python-list mailing list