[Numpy-discussion] dtype truth value
Travis Oliphant
oliphant.travis at ieee.org
Sat Sep 23 13:42:09 CDT 2006
Matthew Brett wrote:
> Hi,
>
> Forgive my ignorance, but why is this?
>
> In [1]:from numpy import *
>
> In [2]:if not dtype('<f8'):
> ...: print 'Truth value is no'
> ...:
> ...:
> Truth value is no
>
Truth value of user-built Python objects is tested by looking at:
1) __nonzero__ method is called to determine truth value.
2) sequence or mapping behavior (then the length is used. If the length
is greater than 0, then True, otherwise it's False.
For data-type objects, there is no __nonzero__ method, but it does have
"mapping" behavior so that fields can be extracted from a data-type
using mapping notation. The "length" of the data-type is the number of
defined fields.
Therefore, if the data-type has no defined fields, it's length is 0 and
it's truth value is "False".
So, you can think of the test as
if dtype(...):
print "Data-type has fields:"
else:
print "Data-type does not have fields:"
-Travis
More information about the Numpy-discussion
mailing list