[Numpy-discussion] comparing string array with None raises ValueError

David M. Cooke cookedm at physics.mcmaster.ca
Thu Jun 24 12:27:07 CDT 2004


On June 24, 2004 01:46 pm, Sebastian Haase wrote:

> In general it must be OK to compare anything with None, right ?
> (BTW, I get the same error with == and !=)

No! Not in general! I learnt this back when Numeric implemented rich 
comparisions; suddenly, lots of my code broke. You don't actually want "is 
this object _equal_ (or not equal) to None", you want "is this object None", 
as None is a singleton.

A contrived example:
>>> class A:
...     def __eq__(self, other): return True
...
>>> a = A()

So, this is bad:
>>> a == None
True

This is good:
>>> a is None
False

In general, if you're testing if something is None, use 'is', not '=='.
>>>q.Mrc.hdr('title') is not None

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca




More information about the Numpy-discussion mailing list