<var> is None vs. <var> == None

Christian Heimes lists at cheimes.de
Fri Jan 23 22:53:39 CET 2009


Jason Scheirer schrieb:
> Yes. I know that there are the PyObject* structs defined for you
> Py_True, Py_False and Py_None in the C level. Confusingly enough, the
> integers -5 through 257 are also singletons where the is test will
> work, but any int out of that range will not.

Small ints are cached. You should use "is True" or "is False" in Python
2.x because user can overwrite the True and False objects. In Python 3.0
both True and False are truly immutable and global singletons -- and
keywords, too. The mutable nature of the name "True" makes a "while
True:" loop sightly slower than a "while 1:" loop, too.

Python 2.5:

>>> None = 1
  File "<stdin>", line 1
SyntaxError: assignment to None
>>> True = 1
>>>

Python 3.0
>>> True = 1
  File "<stdin>", line 1
SyntaxError: assignment to keyword
>>> None = 1
  File "<stdin>", line 1
SyntaxError: assignment to keyword




More information about the Python-list mailing list