Python newbie needs constructive suggestions
Justin Azoff
justin.azoff at gmail.com
Fri Jul 21 22:18:11 EDT 2006
davew-python at cs.haverford.edu wrote:
> What is the idiomatically appropriate Python way to pass, as a "function-type parameter", code that is most clearly written with a local variable?
>
> For example, map takes a function-type parameter:
>
> map(lambda x: x+1, [5, 17, 49.5])
>
> What if, instead of just having x+1, I want an expression that is most clearly coded with a variable that is needed _only_ inside the lambda, e.g. if I wanted to use the name "one" instead of 1:
>
> map(lambda x: (one = 1 x+one), [5, 17, 49.5])
I believe most people would just write something like this:
def something():
#local helper function to add one to a number
def addone(x):
one = 1
return x+one
return map(addone, [5, 17, 49.5])
--
- Justin
More information about the Python-list
mailing list