How to use decorator numpy.vectorize for methods in class
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
On Fri, Jul 16, 2010 at 12:00, Shailendra <shailendra.vikas@gmail.com> wrote:
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
numpy.vectorize does not work on methods, just functions. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (2)
-
Robert Kern
-
Shailendra