[SciPy-dev] Arrays as truth values?

Ryan Gutenkunst rng7 at cornell.edu
Tue Nov 8 18:55:08 CST 2005


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'
...
 >>>

It took me quite a while to track down a bug in my code caused by this 
behavior. I'd at least call this difference from other sequences a 
wart, if not a bug. Perhaps something to consider for new scipy...

Cheers,
Ryan

-- 
Ryan Gutenkunst               |
Cornell Dept. of Physics      |       "It is not the mountain
                               |        we conquer but ourselves."
Clark 535 / (607)255-6068     |        -- Sir Edmund Hillary
AIM: JepettoRNG               |
          http://www.physics.cornell.edu/~rgutenkunst/




More information about the Scipy-dev mailing list