[Numpy-discussion] vectorized methods
Anne Archibald
peridot.faceted at gmail.com
Thu Mar 29 01:16:39 EDT 2007
Hi,
What is the current idiom for vectorizing instance methods? I don't
need vectorization over self. For functions:
from numpy import *
@vectorize
def f(x):
if x>0:
return 1
else:
return 2
print f(array([-1,0,1]))
does the right thing. But for instance methods:
class useless:
def __init__(self,v):
self.v = v
@vectorize
def f(self, x):
if x>self.v:
return 1
else:
return 0
u = useless(0)
print u.f(array([-1,0,1]))
raises an exception:
ValueError: mismatch between python function inputs and received arguments
frompyfunc is possibly more powerful (though it doesn't seem to serve
the purpose either) but the fact that it always returns object arrays
is thoroughly unappealing.
I can create an inner function and vectorize it on every call to f,
but that's not especially efficient or syntactically appealing.
Surely there must be a right way to do this?
Thanks,
Anne M. Archibald
More information about the NumPy-Discussion
mailing list