"/a" is not "/a" ?

Steven D'Aprano steve at pearwood.info
Fri Mar 6 22:12:39 CET 2009


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!


-- 
Steven




More information about the Python-list mailing list