How to pass a numpy scalar to a function nicely in python?

Viewed 67

I have a numpy array a and want to pass its elements to a function func in a loop:

a = np.array([0, 0, 1], dtype='bool')

def func(x):
    if x is True:
        # do something
    else:
        # do something else

for i in np.arange(3):
    res = func(a[i])

I expect a[i] to be <class 'bool'>, but in fact it is <class 'numpy.bool_'> and I have to use a[i].item() every time to convert it to a python scalar, which is prone to error.

for i in np.arange(3):
    res = func(a[i].item())

Is there a better way to do it?

0 Answers
Related