[Numpy-discussion] How to use decorator numpy.vectorize for methods in class

Shailendra shailendra.vikas@gmail....
Fri Jul 16 12:00:22 CDT 2010


Hi All,
I tried to use the decorator @numpy.vectorize for the class method
without success. Can any one point me to the correct way of using it?
I am able to use the numpy.vectorize() sucessfully.
Below is the code with did not work

==================== ================
import numpy
class test:
   @numpy.vectorize  #<========= Using decorator
   def add(self,x,y):
       return x+y

   def operate(self,operation,x,y):
       if operation =='add':
           return self.add(x,y) #<==============trying to call

def test_test():
   test_instance=test()
   x=numpy.arange(1,10)
   y=numpy.arange(11,20)
   print test_instance.operate('add',x,y)

if __name__=='__main__':
   test_test()

==================== ================
  File "test.py", line 10, in operate
    return self.add(x,y)
  File "/usr/local/lib/python2.5/site-packages/numpy/lib/function_base.py",
line 1854, in __call__
    raise ValueError, "mismatch between python function inputs"\
ValueError: mismatch between python function inputs and received arguments
---------------------------------------------------------------------------------------------------------
But the  following modification works
==================== ================
import numpy
class test:
   def add(self,x,y):
       return x+y

   def operate(self,operation,x,y):
       if operation =='add':
           newfunc=numpy.vectorize(self.add) #<============ Explicitly
vectorizing
           return newfunc(x,y)  #<============ calling the vectorized func

def test_test():
   test_instance=test()
   x=numpy.arange(1,10)
   y=numpy.arange(11,20)
   print test_instance.operate('add',x,y)

if __name__=='__main__':
   test_test()
==================== ================

What exactly is happening in case of using decorator?

Thanks,
Shailendra


More information about the NumPy-Discussion mailing list