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

Nick Coghlan ncoghlan at gmail.com
Thu Sep 29 14:03:28 CEST 2011

On Wed, Sep 28, 2011 at 12:39 PM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
>> @init(n=1, lock=Lock())
>> def global_counter():
>>  nonlocal n, lock
>>  with lock:
>>    print(n)
>>    n += 1
> This use-case is no good. Consider:
> _n = 1; _lock = Lock()
> def global_counter():
>    global _n
>    with _lock:
>        print(n)
>        n += 1

And consider what happens as soon as you have two module level
functions that you want to assign a lock to - you end up have to use
clumsy name hacks to avoid collisions in the module namespace. I'll
stick with this as my toy example (although I forgot to actually
acquire the lock in my last post)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list