* Fernando Perez [2004-04-02 00:51]:
Perry Greenfield wrote:
Personally, I prefer 1) since I think there is too much confusion between whether false means an empty (len=0) array (like lists), or an array that contains all zeros, and that it is worth breaking backward compatibility on this (functions should be used to distinguish which meaning is desired).
I tend to prefer keeping compatibility with Python list behavior. I think that in general, numarray arrays should behave different from lists only when truly justified (for example, at least in numeric all ufuncs can be applied to a list of floats as much as to an array, which is nice).
How often does it happen that you have an empty array, which you need to test for? Certainly not as frequently as in case of a list. I think that if
a=ones(3) a==1 array([1, 1, 1]) a==0 array([0, 0, 0])
then I would follow that
if a==0: ... if array([0, 0, 0]): ...
should also _both_ evaluate as "false".
Since anyone who knows python already knows that lists are true based on whether they contain _anything_:
In [9]: a=[]
In [10]: b=[0]
In [11]: if a: ....: print 1 ....:
In [12]: if b: ....: print 1 ....: 1
then I think it's a fair, clean extension to make NumArray behave similarly.
I think a good guiding principle is to keep the number of special cases for arrays at a strict minimum (there are obviously cases where it does make sense to have special cases). Besides, this is an O(1) check, so it doesn't have the dangers of checking for all elements being zero. Such a potentially expensive operation should _definitely_ happen only if explicitly requested, IMHO.
But if the behaviour is unambiguously defined, I see no problem here, because I can as well check the length of a list using len(). BTW, How about >>> a = [[]] >>> if a: ... ? Cheers, Joachim