truth testing new numpy arrays
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
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
On Jan 31, 2006, at 10:43 AM, Travis Oliphant wrote:
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.
Assuming you use None for unset optional arguments, how do you do the test? if myvect: <do something> doesn't work because it is ambiguous if myvect isn't none. if myvect.any(): <do something> doesn't work because None doesn't have an any() method So, do you use: if any(myvect): <do something> or, if myvect is not None: <do something> ? -- Paul -- Dr. Paul S. Ray E-mail: Paul.Ray@nrl.navy.mil Naval Research Laboratory WWW : http://xweb.nrl.navy.mil/ personnel/paulr/ Code 7655 Phone : (202) 404-1619 Washington, DC 20375 AIM : NRLPSR
Paul Ray wrote:
Assuming you use None for unset optional arguments, how do you do the test?
if myvect is not None: <do something> ?
This is *the* canonical, Pythonic approach. -- Robert Kern robert.kern@gmail.com "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
participants (4)
-
Paul Ray -
Robert Kern -
Ryan Krauss -
Travis Oliphant