When to use None

Bernhard Herzog herzog at online.de
Wed Feb 2 14:23:27 CET 2000


Laurence Tratt <tratt at dcs.kcl.ac.uk> writes:

> stevie_deja at my-deja.com wrote:
> 
> > 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?
> 
> In this particular circumstance, both lines of code will always produce the
> same result;

Whether they're equivalent depends on what some_var actually is. If it's
a class instance whose class implements the __cmp__ method it may well
be equal but not identical to None:

>>> class C:
..     def __cmp__(self, other):
..             return cmp(None, other) 
.. 
>>> c = C()
>>> c == None
1
>>> c is None
0
>>> 


-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list