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

Paul Moore p.f.moore at gmail.com
Wed Sep 28 14:15:29 CEST 2011

On 28 September 2011 11:45, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Actually, there are some additional aspects that annoy me:
> - the repetition of the variable name 'n'
> - the scope of the variable name is wrong (since we only need it in
> the inner function)
> - indentation matters (cf try/except/finally)
> - hidden signature for the actual function
> - hoops to jump through to get decent introspection values (e.g. __name__)

Fair points, all (except the scope one, the fact that the scope
extends to cover a couple of lines of boilerplate doesn't really
bother me, but I concede that you're a purist in these matters :-))

> I find the following 5 line toy example:
> from threading import Lock
> def global_counter() [n=1, lock=Lock()]:
>    with lock:
>        print(n)
>        n += 1
> Far more readable than the 10-line closure equivalent:

Agreed entirely. If we're back to talking about line-noise syntax
rather than using "nonlocal" I'm with you all the way (including the
original bit about it still being a large change for a relatively
small benefit...)


More information about the Python-ideas mailing list