Consider this example:
... for i in range(5):
... def g(): return x + i
... yield g
I would expect the value of x used in g to be that at the function
declaration time, as if you've pass g a (x=x) argument, especially
after reading this post: http://lua-users.org/wiki/LuaScopingDiscussion
But:
[5, 5, 5, 5, 5]
Whereas:
[1, 2, 3, 4, 5]
This came up while discussing Python pain points at
I can see how it works now, but I haven't found an easy-to-read
documentation on this.
I guess it's debatable if perhaps every i used in the loop shouldn't
be a different i. It had me fooled, anyways.
Rgds,
Bjorn
>>def funcs(x):
... def g(): return x + i
... yield g
I would expect the value of x used in g to be that at the function
declaration time, as if you've pass g a (x=x) argument, especially
after reading this post: http://lua-users.org/wiki/LuaScopingDiscussion
But:
>>[ fun() for fun in list(funcs(1)) ]
Whereas:
>>[ fun() for fun in funcs(1) ]
This came up while discussing Python pain points at
I can see how it works now, but I haven't found an easy-to-read
documentation on this.
I guess it's debatable if perhaps every i used in the loop shouldn't
be a different i. It had me fooled, anyways.
Rgds,
Bjorn
Comment