You can create a general function and pass the assigned attribute to it:
def calX(a,b, attr):
try:
return getattr(numpy, attr)(a, b, out=a, casting="unsafe")
except AttributeError:
raise Exception("Please enter a valid attribute")
Demo:
>>> import numpy
>>> A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
>>> B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
>>> calX(A, B, 'multiply')
array([ 0, 4, 9, 16], dtype=int16)
>>> calX(A, B, 'subtract')
array([ 0, 1, 6, 12], dtype=int16)
: , return .
A = calX(A, B, 'multiply')