A gotcha: Python pain point?

Diez B. Roggisch deets at nospam.web.de
Tue Jun 12 11:00:51 CEST 2007


Beorn wrote:

> Consider this example:
> 
>   >>> def funcs(x):
>   ...     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

You mean i here, don't you?
> 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)) ]
>   [5, 5, 5, 5, 5]
> 
> Whereas:
> 
>   >>> [ fun() for fun in funcs(1) ]
>   [1, 2, 3, 4, 5]
> 
> This came up while discussing Python pain points at
> http://intertwingly.net/blog/2007/06/04/Python-Pain-Points#c1181602242
> 
> 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

Diez



More information about the Python-list mailing list