[Numpy-discussion] Compare strings.array with None.

Todd Miller jmiller at stsci.edu
Tue Sep 7 07:38:04 EDT 2004


On Mon, 2004-09-06 at 23:11, Shin wrote:
> I got the following strange behavior in numarray.strings.
> Is it intended or a bug? Thanks.
> 
> >>> from numarray import *
> >>> x = [1,2]
> >>> x == None
> 0
> >>> from numarray import strings
> >>> x = strings.array(['a','b'])
> >>> x == None
> TypeError
> (And exit from mainloop.)

Here's what I get:

>>> x == None
Traceback (most recent call last):
...
ValueError: Must define both shape & itemsize if buffer is None

I have a couple comments:

1.  It's a nasty looking exception but I think it should be an
exception.  When 'x' is a CharArray,  what that expression means is to
compute a boolean array where each element contains the result of a
string comparison.  IMHO, in the context of arrays,  it makes no sense
to compare a string with None because it is known in advance that the
result will be False.

2. The idiom I think you're looking for is:

>>> x is None
False

This means that the identity of x (basically the object address) is not
the same as the identity of the singleton object None.  This is useful
for testing function parameters where the default is None.

Regards,
Todd







More information about the NumPy-Discussion mailing list