"/a" is not "/a" ?
gherron at islandtraining.com
Fri Mar 6 21:54:40 CET 2009
Robert Kern wrote:
> On 2009-03-06 14:23, Gary Herron wrote:
>> Robert Kern wrote:
>>> On 2009-03-06 13:46, Gary Herron wrote:
>>>> Emanuele D'Arrigo wrote:
>>>>> Hi everybody,
>>>>> while testing a module today I stumbled on something that I can work
>>>>> around but I don't quite understand.
>>>> *Do NOT use "is" to compare immutable types.* **Ever! **
>>> Well, "foo is None" is actually recommended practice....
>> But since newbies are always falling into this trap, it is still a good
>> rule to say:
>> Newbies: Never use "is" to compare immutable types.
>> and then later point out, for those who have absorbed the first rule:
>> Experts: Singleton immutable types *may* be compared with "is",
>> although normal equality with == works just as well.
> That's not really true. If my object overrides __eq__ in a funny way,
> "is None" is much safer.
> Use "is" when you really need to compare by object identity and not
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.
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.
of even better
Newbies: Never use "is" to compare anything.
This will help them avoid traps, and won't hurt their use of the
language. If they get to a point that they need to contemplate using
"is", then almost be definition, they are not a newbie anymore, and the
rule is still valid.
More information about the Python-list