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

Christian Heimes lists at cheimes.de
Fri Jan 23 23:07:41 CET 2009

John Machin schrieb:
> On Jan 24, 7:48 am, Roger <rdcol... at gmail.com> wrote:
>>> And, just for completeness, the "is" test is canonical precisely because
>>> the interpreter guarantees there is only ever one object of type None,
>>> so an identity test is always appropriate. Even the copy module doesn't
>>> create copies ...
>> Does the interpreter guarantee the same for False and True bools?
> Why do you think it matters? Consider the circumstances under which
> you would use each of the following:
>    if some_name == True:
>    if some_name is True:
>    if some_name:

The three lines translate (roughly) into:

    if some_name.__eq__(True):
    if id(some_name) == id(True):
    if some_name.__nonzero__(): # may check for __len__() != 0, too.

In almost every case (99.99%) you want the last variant. The second variant

More information about the Python-list mailing list