I have a function that takes two numpy arrays as an argument, and I need to perform some basic checks on the elements of the array inside the function. Something like this:
import numpy as np
xdata = np.arange(0,10)
ydata = np.arange(0,100,10)
def div(x, y):
# if y == 0:
# return 0
return x/y
zdata = div(xdata, ydata)
I understand why the commented lines cause an error, but I don't know if there is a simple way to make them work.
I am using an external module that calls this function, so the data checking has to be inside the function and the type must be a numpy array. A grossly simplified version:
def div(x, y):
return x/y
foo.set_function(div)
z = foo.bar()
# In external module
class foo:
def set_function(f):
self.func = f
def bar():
x = np.ndarray()
y = np.ndarray()
return self.f(x, y)
Thanks in advance!