[Numpy-discussion] preferred way of testing empty arrays
chris.barker at noaa.gov
Mon Jan 30 19:35:05 EST 2012
On Fri, Jan 27, 2012 at 1:29 PM, Robert Kern <robert.kern at gmail.com> wrote:
> Well, if you really need to do this in more than one place, define a
> utility function and call it a day.
> def should_not_plot(x):
> if x is None:
> return True
> elif isinstance(x, np.ndarray):
> return x.size == 0
> return bool(x)
I tend to do things like:
if x is None:
x = np.asarray(x)
if b.size == 0:
it does mean you need to check for None later anyway, but I like to
convert to an array early in the process -- then you know you have
either an array or None at that point.
NOTE: you could also raise and handle an exception instead.
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion