[Numpy-discussion] Vectorizing a class method

Anne Archibald peridot.faceted@gmail....
Wed Feb 14 13:11:55 CST 2007


On 14/02/07, Wojciech Śmigaj <puddleglum@o2.pl> wrote:
> Timothy Hochberg wrote:
> > On 2/14/07, *Wojciech Śmigaj* <puddleglum@o2.pl
> > <mailto:puddleglum@o2.pl>> wrote:
> >>     I have a question about the vectorize function. I'd like to use it to
> >>     create a vectorized version of a class method. I've tried the following
> >>     code:
> >>
> >>        from numpy import *
> >>
> >>        class X:
> >>            def func(self, n):
> >>                return 2 * n  # example
> >>            func = vectorize(func)

The appropriate spelling for this, in modern pythons, is

class X:
  @vectorize
  def func(self, n):
    return 2*n

Not that it makes it work any better.

There's no reason vectorize couldn't be made to do the Right Thing
when handed (bound and unbound) methods, for example by examining
X.func.im_self, X.func.im_class and X.func.im_func attributes. I can't
imagine a situation where a method would want to be vectorized over
its self parameter, or at least, not by default.

Anne


More information about the Numpy-discussion mailing list