"/a" is not "/a" ?

Steven D'Aprano steve at pearwood.info
Fri Mar 6 22:22:23 CET 2009

Gary Herron wrote:

>> Huh? How am I supposed to compare immutable types for identity then? Your
>> bizarre instruction would prohibit:
>> if something is None
> Just use:
>   if something == None
> It does *exactly* the same thing.


"something is None" is a pointer comparison. It's blindingly fast, and it
will only return True if something is the same object as None. Any other
object *must* return False.

"something == None" calls something.__eq__(None), which is a method of
arbitrary complexity, which may cause arbitrary side-effects. It can have
false positives, where objects with unexpected __eq__ methods may return
True, which is almost certainly not the intention of the function author
and therefore a bug.

> If they use a couple "something==None" instead of "something is None"
> in their code while learning Python, it won't hurt, 

Apart from the subtle bugs they introduce into their code.

> and they can change 
> their style when they understand the difference.  And meanwhile they
> will skip  traps newbies fall into when they don't understand these
> things yet.

How about teaching them the right reasons for using "is" instead of giving
them false information by telling them they should never use it?


More information about the Python-list mailing list