Explanation of list reference

Ned Batchelder ned at nedbatchelder.com
Sun Feb 16 11:29:53 EST 2014


On 2/16/14 9:00 AM, Rustom Mody wrote:
> On Saturday, February 15, 2014 7:14:39 PM UTC+5:30, Marko Rauhamaa wrote:
>> Mark Lawrence:
>
>>> I have no interest in understanding object identity, I can write code
>>> quite happily without it.
>
>> Luckily, what we are now debating is mostly terminology and points of
>> view where the outcomes are unaffected.
>
>> However, as an example, it is important to know if you should write:
>
>>     if x is not None:
>>         ...
>
>> or if
>
>>     if x != None:
>>         ...
>
>> is more robust.
>
> Yes This is my main beef:
> Not that both are possible but that the first is *recommended* and the second not.
>

I'm not sure why you don't like the recommendation, or if you just want 
people to be more explicit about why it is recommended.  My main reason 
for preferring "x is not None" is that if x's class defines __ne__ 
incorrectly, "x != None" can come out wrong.  And yes, I have actually 
debugged problems where that was the root cause.

If you use "x is not None", nothing about x's class can interfere with 
the correct operation.

> Something like a C compiler manual advising:
>     You can write x*8 but its better to drop out into asm and write
>     shl $3, %eax
>


-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list