Python is Considered Harmful
Alan Offer
alan_offer at hotmail.com
Thu Oct 30 10:01:06 EST 2003
Isaac To <kkto at csis.hku.hk> wrote in message news:<7ioew3xjh1.fsf at enark.csis.hku.hk>...
> I believe the behaviour can be fixed rather easily by some directives, say
> making it
>
> map(lambda f: f(1), [lambda x: x+(*i) for i in range(3)])
>
> where the * construct would get the reference of i at function definition
> time rather than at function invocation time (anyone can point me to an
> PEP?).
We can essentially do this by passing i to a function that then returns the
desired function. So to make flist with the functions that mike420 wanted,
we can use:
flist = map(lambda i: (lambda x: x+i), range(3))
Now [f(1) for f in flist] is [1, 2, 3] as he expected.
More information about the Python-list
mailing list