Python is Considered Harmful

Marco Antoniotti marcoxa at cs.nyu.edu
Thu Oct 30 16:52:34 EST 2003



Alan Offer wrote:

> 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.

This is good and I stand corrected then.  At least you can achieve this 
with Python.

Cheers
--
marco





More information about the Python-list mailing list