Excellent explanation by Mark Wooding. I would only like to add that
the standard pythonic idiom in such cases seems to be the (ab)use of a
default argument to the function, because these get evaluated at the
definition time:
def gen0():
for i in range(3):
def gen1(i = i):
Dieter Maurer <[EMAIL PROTECTED]> wrote:
> I met the following surprising behaviour
[code moved until later...]
> The apparent reason is that the free variables in nested generator
> definitions are not bound (to a value) at invocation time but only at
> access time.
No. This is about the diff
On Jul 3, 9:20 pm, "Dieter Maurer" <[EMAIL PROTECTED]> wrote:
> The apparent reason is that the free variables in
> nested generator definitions are not bound (to a value) at invocation
> time but only at access time.
That's what it is supposed to do. Welcome to a dynamic
language.
Raymond
--
I met the following surprising behaviour
>>> def gen0():
... for i in range(3):
... def gen1():
... yield i
... yield i, gen1()
...
>>> for i,g in gen0(): print i, g.next()
...
0 0
1 1
2 2
>>> for i,g in list(gen0()): print i, g.next()
...
0 2
1 2
2 2
If this is not a bug, it is