When to use None

Michael Hudson mwh21 at cam.ac.uk
Wed Feb 2 10:47:12 EST 2000


Bernhard Herzog <herzog at online.de> writes:

> 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
> >>> 
> 

Yikes! That's impressively devious.

I-always-*knew*-there-was-a-reason-I-always-use-"is"-ly y'rs
Michael



More information about the Python-list mailing list