[Numpy-discussion] preferred way of testing empty arrays
Mon Jan 30 18:35:05 CST 2012
On Fri, Jan 27, 2012 at 1:29 PM, Robert Kern <email@example.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
More information about the NumPy-Discussion