Is there a function that applies list of functions to a value?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Aug 29 22:18:58 EDT 2013


On Thu, 29 Aug 2013 14:27:23 -0700, fp2161 wrote:

> Chris, call me a snob, but I resent using lambdas (aren't they usually
> considered odd/bad practice in python?)

Only among people who dislike functional programming idioms. Like GvR.

It is true that lambda functions are slightly restricted compared to 
"normal" functions: they are limited to a single expression, and they all 
share the same name '<lambda>', which if you have a lot of them can make 
debugging annoying. So *overuse* of lambdas is considered bad form. But 
for callbacks and such, they're fine.

It is frowned upon to *directly* bind a lambda to a name, as in this:

plusOne = lambda x: x+1

instead of:

def plusOne(x): return x+1


which is fair enough for production code, but at the interactive 
interpreter (and throwaway code) I'll continue to feel free to assign 
anonymous functions to names just as I assign anonymous ints and 
anonymous lists to names :-)

I say "directly" because this of course is allowed:

funcs = [lambda x: x+1, lambda x: x*2, lambda x: x**3]
for func in func:
    ...


and *much* better than having to predefine plusOne, timesTwo, powerThree 
functions.



-- 
Steven



More information about the Python-list mailing list