"/a" is not "/a" ?

Gary Herron gherron at islandtraining.com
Fri Mar 6 17:38:34 EST 2009


Steven D'Aprano wrote:
> Gary Herron wrote:
>
>   
>> Robert Kern wrote:
>>     
> ...
>   
>>> Use "is" when you really need to compare by object identity and not
>>> value.
>>>       
>> But that definition is the *source* of the trouble.  It is *completely*
>> meaningless to newbies.   Until one has experience in programming in
>> general and experience in Python in particular, the difference between
>> "object identity" and "value" is a mystery.
>>     
>
> Then teach them the difference, rather than give them bogus advice.
>
>
>   
>> So in order to lead newbies away from this *very* common trap they often
>> fall into, it is still a valid rule to say
>>
>>     Newbies: Never use "is" to compare immutable types.
>>     
>
> Look in the standard library, and you will see dozens of cases of
> first-quality code breaking your "valid" rule.
>
> Your rule is not valid. A better rule might be:
>
> Never use "is" to compare equality.
>
> Or even:
>
> Never use "is" unless you know the difference between identity and equality.
>
> Or even:
>
> Only use "is" on Tuesdays.
>
> At least that last rule is occasionally right (in the same way a stopped
> clock is right twice a day), while your rule is *always* wrong. It is never
> correct to avoid using "is" when you need to compare for identity.
>
>   
>> of even better
>>
>>     Newbies: Never use "is" to compare anything.
>>     
>
> Worse and worse! Now you're actively teaching newbies to write buggy code!
>   

Nonsense.  Show me "newbie" level code that's buggy with "==" but 
correct with "is". 

However, I do like your restatement of the rule this way:
    Never use "is" unless you know the difference between identity and 
equality.
That warns newbies away from the usual pitfall, and (perhaps) won't 
offend those
who seem to forget what "newbie" means.

Gary Herron





>
>   




More information about the Python-list mailing list