'is not' or '!='

Rustom Mody rustompmody at gmail.com
Mon Aug 18 22:29:11 EDT 2014


On Tuesday, August 19, 2014 2:05:01 AM UTC+5:30, ElChino wrote:
> A newbie question to you; what is the difference between statements 
> like:
>   if x is not None:
> and
>  if x != None:

> Without any context, which one should be preferred?
> IMHO, the latter is more readable.

Here is Terry Reedy, a python-dev, from Mar 2, on this list on 'is'

| The 'is' operator has three uses, two intended and one not. In
| production code, 'is' tests that an object *is* a particular singular
| object, such as None or a sentinel instance of class object². In test
| code, 'is' can also be used to test details of a particular
| implementation, such as pre-allocation of small ints. New python
| programmers also use it to confuse themselves. 

which I would summarize by "if you are a beginner you dont want to
deal with is"¹

As for Marko's dollar bill example: 
I find it very plausible and still more misleading because:

1. We write computer programs because we want to understand/manipulate
something outside the computer -- 'reality'
2. The computer program never deals with that reality directly but at one
or more removes -- it handles models

eg. You go to a real-estate agent to buy an apartment and see two
plaster of paris models is his office.  Different cases may emerge:

a. The models are for two different projects -- one on the North and
one on the East side of town
b. They are two facades of the same project

OTOH the fact that there are two distinct plaster of paris objects in
the agent's office is true but mostly irrelevant.
ie if the agent says "These two ARE the same" he means they are different views 
of the same building.
IOW python's use of the word 'is' is misleading and unfortunate 

¹ "... is None" is an exception -- unfortunate in my view.
Treat the 'is None' as atomic write it but and dont look inside!

² Same as case 3



More information about the Python-list mailing list