[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Carl Matthew Johnson cmjohnson.mailinglist at gmail.com
Fri Sep 30 05:45:32 CEST 2011


On Sep 29, 2011, at 5:35 PM, Ron Adam wrote:

>   @set_closure(x=0)
>   def adder(y):
>       nonlocal x
>       x += y
>       return x

Won't work as written because the nonlocal will cause an error before the decorator is applied.

The other day, I suggested we add the ability for decorators to have a __prepare__ method that creates the namespace the function being created will use, but it was shot down as impractical. It would however do what you want to do here.


More information about the Python-ideas mailing list