[Numpy-discussion] vectorized methods

Anne Archibald peridot.faceted@gmail....
Thu Mar 29 00:16:39 CDT 2007


What is the current idiom for vectorizing instance methods? I don't
need vectorization over self. For functions:

from numpy import *

def f(x):
    if x>0:
        return 1
        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

    def f(self, x):
        if x>self.v:
            return 1
            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?

Anne M. Archibald

More information about the Numpy-discussion mailing list