Python is Considered Harmful

Isaac To kkto at csis.hku.hk
Thu Oct 30 21:06:53 EST 2003


>>>>> "Alan" == Alan Offer <alan_offer at hotmail.com> writes:

    Alan> Isaac To <kkto at csis.hku.hk> wrote in message
    Alan> 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?).

    Alan> We can essentially do this by passing i to a function that then
    Alan> returns the desired function.  So to make flist with the functions
    Alan> that mike420 wanted, we can use:

    Alan> flist = map(lambda i: (lambda x: x+i), range(3))

    Alan> Now [f(1) for f in flist] is [1, 2, 3] as he expected.

Ah... that means

  map(lambda f: f(1), [(lambda i: lambda x: x+i)(i) for i in range(3)])

Seems make sense to me.

Regards,
Isaac.




More information about the Python-list mailing list