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 Best, Matthew
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 userbuilt 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 datatype objects, there is no __nonzero__ method, but it does have "mapping" behavior so that fields can be extracted from a datatype using mapping notation. The "length" of the datatype is the number of defined fields. Therefore, if the datatype 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 "Datatype has fields:" else: print "Datatype does not have fields:" Travis
participants (2)

Matthew Brett

Travis Oliphant