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

Devin Jeanpierre jeanpierreda at gmail.com
Thu Sep 29 19:10:39 CEST 2011


> 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)

Surely the solution is a new namespace (that's still publicly accessible)?

It's absolutely pannoying that you have to prefix the globals like in
C, but deftime-assigned nonlocals are less convenient in other
respects too. e.g. try sharing a lock among multiple functions, or try
testing the usage of a lock, etc.

Devin

On Thu, Sep 29, 2011 at 8:03 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 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)
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>



More information about the Python-ideas mailing list