[SciPy-dev] Arrays as truth values?

Perry Greenfield perry at stsci.edu
Wed Nov 9 10:23:07 CST 2005

On Nov 8, 2005, at 8:05 PM, Travis Oliphant wrote:

> Ryan Gutenkunst wrote:
>> On Nov 8, 2005, at 10:35 AM, Perry Greenfield wrote:
>>> Robert has already pointed out that lots of people want == to result 
>>> in
>>> an array of booleans (most I'd argue) rather than a single boolean
>>> value.
>> I'm coming late to this discussion, but I'd like to mention that a
>> similar issue with the old scipy burned me just today.
>> Guido's PEP 8 Style Guide suggests:
>>     - For sequences, (strings, lists, tuples), use the fact that empty
>>       sequences are false, so "if not seq" or "if seq" is preferable
>>       to "if len(seq)" or "if not len(seq)".
>> However, an old scipy array containing any number of zeros is 'False',
>> as illustrated below. (I haven't tried this on new scipy.)
>>>>> scipy.__version__
>> '0.3.3_303.4601'
>>>>> if [0]:
>> ...  print 'hello'
>> ...
>> hello
>>>>> if scipy.array([0, 0]):
>> ...  print 'hello'
>> ...
> So, do we want empty arrays to return false and not return an error?
> This is definitely a possibility.

I'm not sure what is being suggested here. One interpretation is that 
empty arrays are false and non-empty arrays are true. That doesn't 
really solve the problems raised before regarding misinterpretations of 
these logical expressions.

The other interpretation is that empty arrays are false and anything 
else raises an exception. This would be really bizarre IMHO (writing a 
logical test assuming that it is always false and having to trap it if 
it isn't?).

I don't think any decision is going to remain entirely Pythonic since 
we are seeing colliding issues that will cause some sort of conflict 
with established principles one way or another.

Look at it this way, we haven't met the above expectation for empty 
sequences for a long time. How much of a problem has that been compared 
to the other problems?

Another alternative is to ask WWGD. Since he's still around we can ask 
him ;-). I tend to think practicality beats purity on this issue 


More information about the Scipy-dev mailing list