Python is Considered Harmful
Jegenye 2001 Bt
jegenye2001 at fw.hu
Mon Oct 27 13:39:40 EST 2003
Thank you, Alex, for clarifing this issue for me..
So this was actually like the famous equation
"9*6=42"
:-)
It is correct (or incorrect) if you look at it in the right (or wrong) way.
Best,
Miklós
--
PRISZNYÁK Miklós
---
Jegenye 2001 Bt. ( mailto:jegenye2001 at parkhosting.com )
Egyedi szoftverkészítés, tanácsadás
Custom software development, consulting
http://jegenye2001.parkhosting.com
Alex Martelli <aleax at aleax.it> wrote in message
news:M67nb.359375$R32.11848659 at news2.tin.it...
> Isaac To wrote:
> ...
> > Did you really mean
> >
> > map(lambda f: f(1), [lambda x: x+i for i in range(3)])
> >
> > ? If so, it does return [3, 3, 3]. The only problem here is that
Python
> > variables are name lookups over dictionaries, and as such the value of
"i"
> > is not coined into the lambda. Rather it is "external", binded
> > dynamically. 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)])
> >
> No new "directive" needed: it's easy to get "snap-shots" of current
> values of names into a lambda or other function:
>
> map(lambda f: f(1), [lambda x, i=i: x+i for i in range(3)])
>
> The "i=i" gets the (outer) value of i at function-definition time and
> injects it as local variable i in the lambda. You can also rename just
> as easily (and sometimes it's clearer):
>
> map(lambda f: f(1), [lambda x, k=i: x+k for i in range(3)])
>
>
>
> Alex
>
More information about the Python-list
mailing list