[SciPy-user] truth testing new numpy arrays

Ryan Krauss ryanlists at gmail.com
Tue Jan 31 09:23:53 CST 2006


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.

Is there a best way to handle optional arrays in function
specifications?  (I am slightly addicted to Python's boolean testing
for empty objects.)

Ryan



More information about the SciPy-user mailing list