'is not' or '!='

Steven D'Aprano steve at pearwood.info
Tue Aug 19 04:36:26 EDT 2014


On Mon, 18 Aug 2014 19:29:11 -0700, Rustom Mody wrote:

> 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.

Hah :-)

You'll notice that use-case #2, testing implementation details, is just a 
special case of #1, testing that a value is a specific instance.


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

In general, correct. Actually, even experts should hardly ever need to 
use `is`. And as you point out, "is None" is an exception.


> As for Marko's dollar bill example:
> I find it very plausible and still more misleading because:
[...snip example of plaster-of-paris models...]
> 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

The English word "is" is the third-person singular simple present 
indicative form of "be", quoting Wiktionary:

http://en.wiktionary.org/wiki/is#English

The word "be" (is, was, been, will be, has been, etc.) has many different 
meanings and usage, including:

- mathematical equality, "two times three is six";

- metaphorical equivalence, "he is a dumb ox";

- linking the subject to a count or measurement, "this building is 
  300 years old";

- to connect a noun to an adjective, "my shirt is wrinkled";

- to connect the subject noun to a noun or noun phrase, "she is
  a petty dictator";

- to indicate that the subject and object are the same, "Elizabeth 
  Windsor[1] is the Queen of England";

and quite a few more.

http://en.wiktionary.org/wiki/be#English


There is no requirement that the word "is" can only be used in a 
programming language if it reflects all of these meanings. After all, we 
don't expect that the jargon terms "function", "object", "class", 
"method", "procedure" as used by programming languages have *precisely* 
the same meanings as used in plain English.

A programming language is entitled to choose the meaning of its keywords, 
hopefully they would be familiar meanings not radically dissimilar from 
the normal plain English meaning (or whatever language the word is taken 
from). Hypertalk, and its descendants, use "is" as a synonym for 
equality, so that "x is 2" and "x = 2" mean exactly the same thing. 
Python, on the other hand, gives "is" a specific meaning: are the two 
operands the same instance? Although all men and women are equal, only 
one such person is Elizabeth II Queen of England.




[1] It isn't clear or obvious what the Queen[2] would use as a surname on 
the unlikely event she needed one. Even the official website of the UK 
royal family is unclear as to what Elizabeth II would use as a surname 
since her marriage. Prior to her marriage, she used "Elizabeth Windsor" 
on those rare occasions that "Princess Elizabeth" or "Queen Elizabeth II" 
was not sufficient. Her children, should they need a surname, use  
Mountbatten-Windsor. Prince William and Harry used the surname 
"Wales" (for their father's title, Prince of Wales) upon joining the 
military. The whole thing is oh so very complicated.

http://www.royal.gov.uk/ThecurrentRoyalFamily/TheRoyalFamilyname/Overview.aspx

Why do I know so much about the British royal family???


[2] Of the UK, Australia, and a few other places, but not Denmark, or any 
other place with a Queen.



-- 
Steven



More information about the Python-list mailing list