[Python-ideas] Tweaking closures and lexical scoping to include the function being defined
Jan Kaliszewski
zuo at chopin.edu.pl
Thu Sep 29 00:32:35 CEST 2011
Nick Coghlan dixit (2011-09-28, 05:59):
> def accumulator():
> def incr(x) [tally=0]:
> tally += x
> return tally
> return incr
>
> As the rough equivalent of today's:
>
> def accumulator():
> tally = 0
> def incr(x):
> nonlocal tally
> tally += x
> return tally
> return incr
IMHO such a syntax (or a special or non-special decorator) should simply
allow to add free variable(s) to a function. Then, if we want to modify
such a variable in the function body we should use nonlocal, as with
today's closures:
def accumulator():
def incr(x) [tally=0]:
nonlocal tally
tally += x
return tally
return incr
But we would not need to use nonlocal for read-only access, as with
today's closures:
def my_func() [lock=Lock()]:
with lock:
"foo"
Cheers.
*j
More information about the Python-ideas
mailing list