I think the wims answer really talks about it mostly, but maybe this makes the differences clearer.
The scalar returned by numpy should with array[0] should be (almost?) Fully compatible with the standard python float:
a = np.ones(2, dtype=float) isinstance(a[0], float) == True # even this is true.
For the most part, an array of size 1 is already compatible with both a scalar and a list, although, for example, it is a mutable object, and float is not:
a = np.ones(1, dtype=float) import math math.exp(a)
So, if the user does not need to know about this, I think the first is good, the second is dangerous.
You can also use np.asscalar(result) to convert an array of size 1 (any dimension) to the correct python scalar:
In [29]: type (np.asscalar (a [0])) Out [29]: float
If you want to make sure that there are no surprises for a user who does not need to know about numpy, you will need to at least get element 0 if a scalar has been passed. If the user needs to know, just the documentation is probably just as good.
seberg Sep 24 2018-12-12T00: 00Z
source share