I would like to use numpy.frompyfunc to generate an unbound ufunc from an unbound member method. My concrete but failing attempts look like
class Foo(object):
def f(x,y,z):
return x + y + z
Foo.g = numpy.frompyfunc(Foo.f,3,1)
i = Foo()
i.g(5,6,7)
where the last line fails with
“TypeError: unbound method f() must be called with Foo instance as first argument (got int instance instead)”. That error makes sense to me as Foo.f is unbound.
Despite it being deprecated, I thought I’d give new.instancemethod a shot:
import new
Foo.h = new.instancemethod(numpy.frompyfunc(Foo.f,3,1),None,Foo)
j = Foo()
j.h(5,6,7)
where the last line now fails with “TypeError: return arrays must be of ArrayType” which I don’t understand.
My goal is to monkey patch Foo with ufunc-ready versions of its members. They must be member methods as they depend on a Foo instance’s state (though that dependency is not shown here).
Any suggestions?
Add a
gmethod toFoo:Calling
gfrom an instance ofFoo: