[SciPy-user] truth testing new numpy arrays

Travis Oliphant oliphant.travis at ieee.org
Tue Jan 31 09:43:38 CST 2006


Ryan Krauss wrote:

>I have a style question.  With the old scipy/Numeric, when I had a
>vector I wanted to be an optional variable in a function I would set
>is default to "myvect=[]".  Then when I wanted to test whether or not
>the variable had been specified, I just checked "if myvect:".  With
>numpy arrays, this produces a message about ambiguous results for
>testing arrays and says I should use myvect.any().  The problem is
>that empty lists don't have an "any" method.  I could set the optional
>arrays explicitly to None or I could check if it" ==[]", but both of
>these are slightly more fragile (if I accidentally pass an empty list
>instead of None it would cause problems).  I could also set the
>optional array to "array([])" and always test "any()", but that is
>more typing.
>  
>
I typically use None for optional array arguments.  

But, you could do what your doing and test

if any(myvect):
    <do something>
else:
    <do something else>

But, I think the None test is less prone to error.  

-Travis



More information about the SciPy-user mailing list