python hack of the day -- "listable" functions

Tim Peters tim_one at email.msn.com
Wed May 12 21:23:44 EDT 1999


[Michael Vanier]
> Having nothing better to do one day, I whipped up a quick class that
> transforms functions into "listable" functions i.e. functions that
> automatically map themselves over a sequence.  The idea was stolen from
> Mathematica, but it's amazing how easy it is to implement this in python.

Not amazing:  Guido had this very thing in mind from the first day.  It's
just taken a decade for someone to finally write it <wink>.

> Function-objects--is-there-anything-they-can't-do?-ly y'rs,

Actually no -- at least not among possible things.  For impossible things,
sometimes you need two classes and a __getattr__.

A tip:

>         # Construct a tuple for the argument list.  It must have
>         # the form: (func, <sequence>).
>         arglist = (self.func,) + args
>         result = apply(map, arglist)

You can replace that block with:

    result = map(self.func, args)

i-don't-*always*-strive-to-make-things-more-obscure<wink>-ly y'rs  - tim






More information about the Python-list mailing list