[Numpy-discussion] vectorized function inside a class

Stefan van der Walt stefan at sun.ac.za
Wed Aug 8 11:50:00 EDT 2007


Hi Mark

On Wed, Aug 08, 2007 at 03:37:09PM -0000, mark wrote:
> I am trying to figure out a way to define a vectorized function inside
> a class.
> This is what I tried:
> 
> class test:
> 	def __init__(self):
> 		self.x = 3.0
> 	def func(self,y):
> 		rv = self.x
> 		if y > self.x: rv = y
> 		return rv
> 	f = vectorize(func)
> 
> 
> >>> m = test()
> >>> m.f( m, [-20,4,6] )
> array([ 3.,  4.,  6.])

Maybe you don't need to use vectorize.  How about

def func(self,y):
    y = y.copy()
    y[y <= self.x] = self.x
    return y

Cheers
Stéfan



More information about the NumPy-Discussion mailing list