conventions/requirements for 'is' vs '==', 'not vs '!=', etc

Christian Heimes lists at cheimes.de
Mon May 19 17:04:56 EDT 2008


Luis Zarrabeitia schrieb:
> Personally, I like to use "is" with singletons. I find it easier to type and 
> read "if param is None" than "if param == None", but some python developers 
> dislike the first one because of the "dark magic" involved in knowing that 
> None is a singleton.

Testing for None is the only common case where you should *not* use ==
comparison. If you want to check if something is None then do "if
something is None". Easy, isn't it? :]

Look, what Python does for the various way (simplified):

"if a == None:" -> "if a.__cmp__(None)"

"if a is None" -> "if id(a) == id(None)"

"if a:" -> "if a.__nonzero__()" / "if a.__len__()"

Christian




More information about the Python-list mailing list