Apply Callable to NDArray

93 Views Asked by At

I am new to Python and I have done a fair share of just trying things to see if it works. In this case, when I apply a Callable to an NDArray, I get a result, just not what I expected.

from typing import Callable
from typing import Tuple
import numpy as np

callable : Callable[[float], Tuple[float, float]] = lambda x : (x , x + 1)
array : np.ndarray = np.asarray([0, 1, 2, 3])
result = callable(array)
print(result)

I expected (or rather hoped) to get an iterable of tuples where each tuple is the output of the callable applied to a float. What I got was a tuple of arrays:

(array([0, 1, 2, 3]), array([1, 2, 3, 4]))

What is actually happening? (Why should I expect the results I actually got?)

1

There are 1 best solutions below

0
Little Endian On

To summarize the comments, the typing annotation for callable was misleading and the lambda was just applied to the array itself - the same as if I did result=(array, array+1). Nothing mysterious going on. Thanks for the help.