Vectorized functions
Christian Gollwitzer
auriocus at gmx.de
Thu Aug 11 02:49:56 EDT 2016
Am 11.08.16 um 06:02 schrieb Steven D'Aprano:
> Here's a neat little decorator which decorates a function with a special
> callable attribute "v" which operates somewhat like Julia's dot syntax:
>
> def vectorize(func):
> def v(seq, **kw):
> return [func(x, **kw) for x in seq]
> func.v = v
> return func
>
>
>
> py> @vectorize
> ... def add2(x):
> ... return x+2
> ...
> py> add2.v([100, 200, 300, 400])
> [102, 202, 302, 402]
>
>
Neat. I suspect, that in addition to the higher-level syntax, it also
improves the speed in Julia, because the loop is implicit and it
compiles to native code using LLVM. Numpy's ufunc comes to mind - there
is also a vectorizer for Python functions
http://docs.scipy.org/doc/numpy/reference/generated/numpy.frompyfunc.html#numpy.frompyfunc
Christian
More information about the Python-list
mailing list