Odd closure issue for generators

Dave Angel davea at ieee.org
Fri Jun 5 00:06:38 EDT 2009


Carl Banks wrote:
> <snip>
>
> The way to handle the issue you are seeing is to create a new scope
> with a variable the remains at the value you want to close upon:
>
> create_const_function(value):
>     def func():
>         return value
> c =create_const_function(i) for i in range(11, 16))
>
> Or you can do it the slacker way and use a default argument:
>
> c =lambda i=i: i for i in range(11, 16))
>
>
> Carl Banks
>
>   
I agree with most of what you say, but I think you missed the last line 
of the function:

create_const_function(value):
    def func():
        return value

    return func




More information about the Python-list mailing list