[Python-ideas] Tweaking closures and lexical scoping to include the function being defined
Paul Moore
p.f.moore at gmail.com
Thu Sep 29 17:15:14 CEST 2011
On 29 September 2011 15:05, Paul Moore <p.f.moore at gmail.com> wrote:
> On 29 September 2011 14:30, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>> @nonlocal(n=0, lock=threading.Lock())
>> def global_counter():
>> with lock:
>> n += 1
>> return n
>>
>> It's presence in the decorator list hints that this is something
>> happening outside the functions ordinary local scope, as well as
>> indicating when the initialisation happens. The 'nonlocal' then ties
>> in with how they behave from the point of view of the code in the
>> function body.
>
> I agree, this is a nice option. My biggest discomfort with it is the
> idea of a magic decorator name handled specially by the compiler (and
> worse still, it's a keyword so it's syntactically very weird indeed).
> I had similar discomfort over the new super, but I could get over that
> by simply assuming that super was a normal function that was just more
> magical than I understood. "@keyword" decorators don't even fit into
> my model of valid syntax :-(
>
> That said, I like it otherwise. The above says to me "the following
> values are nonlocal to this function", which I can read exactly the
> way it actually works. Whether that's a result of a week's immersion
> in Nick's propaganda, I can't say, though :-)
Actually, while we're using keywords as decorator names,
@with(n=0, lock=threading.Lock())
def global_counter():
with lock:
n += 1
return n
reads well to me, and avoids the technical/jargon word "nonlocal". Let
the @keyword floodgates open :-)
Paul.
More information about the Python-ideas
mailing list