[Python-ideas] Programming recommendations (PEP 8) and boolean values

Ned Batchelder ned at nedbatchelder.com
Thu Aug 9 16:23:40 CEST 2012


On 8/9/2012 4:09 AM, Rob Cliffe wrote:
>
> On 09/08/2012 06:48, Georg Brandl wrote:
>> On 08.08.2012 23:56, Ned Batchelder wrote:
>>>
>>> On 8/8/2012 4:29 PM, Georg Brandl wrote:
>>>> For None, "==" and "is" are equivalent, because no other object is 
>>>> equal
>>>> to None.  For True and False, this is different, and using "is" 
>>>> here is
>>>> a very stealthy bug.
>>> It's easy to make other objects == None, by writing buggy __eq__
>>> methods.  That doesn't happen much, but it's easier with __ne__, where
>>> the negated logic is easy to screw up.  I've seen it happen. Use "is
>>> None", not "== None".
>>
>> That's true indeed.
>>
>> Georg
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
>>
> Surely that's a reason to fix the buggy __eq__ and __ne__ methods, not 
> to avoid using them.
> (Sorry Georg I accidentally replied to you not to the list.)

Of course you should fix those.  But if the two methods are "equivalent" 
except one isn't susceptible to this sort of error, why not recommend 
and use the more robust technique?  Use "is None", not "== None".

--Ned.

> Rob Cliffe
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>




More information about the Python-ideas mailing list