When to use None

Gordon McMillan gmcm at hypernet.com
Tue Feb 1 14:19:49 EST 2000


Steve writes:
> 
> Would anyone explain to me where there is a difference between the
> following two calls:
> 
> if some_var == None :  print 'var is none'
> 
> if some_var is None :  print 'var is none'
> 
> Can '== None' only be used in certain circumstances?

Normally "==" (which is intended to check object contents, 
but needs support in the object to do so) is safer than "is" 
(which checks if the two operands are the same object). If the 
object does not implement __cmp__, "==" falls back to "is". 

But None is a singleton object, so "is None" is faster than "== 
None" while being semantically identical.

- Gordon




More information about the Python-list mailing list