A gotcha: Python pain point?
bjorns at gmail.com
Wed Jun 13 02:17:35 CEST 2007
On Jun 12, 5:00 pm, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> Beorn wrote:
> > I can see how it works now, but I haven't found an easy-to-read
> > documentation on this.
> This has been discussed here very often. Python closures do capture the
> names, not the values. If you want a value at a certain point, you need to
> bind in the generated function scope by passing it as parameter. Like this:
> def funcs(x):
> for i in xrange(5):
> def g(i=i): return x + i
> yield g
Well, I would hope to see this documented somewhere prominently;
looking at the PEPs didn't give me much of a clue. I'm not even sure
how to describe the problem, so formulating search queries becomes
How does other languages, like Ruby, work? Sam (see original link
about pain points) was very surprised that the for loop didn't
introduce a new scope (is that the right word?) for each iteration?
More information about the Python-list