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

Nick Coghlan ncoghlan at gmail.com
Tue Sep 27 21:53:39 CEST 2011


On Tue, Sep 27, 2011 at 1:10 PM, Guido van Rossum <guido at python.org> wrote:
> On Tue, Sep 27, 2011 at 3:14 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> You can argue that nonlocal is about lifetime instead of visibility
> until you're blue in the face, but I still disagree. It is first and
> foremost about visibility. Lifetime is secondary. Note how a nonlocal
> variable may actually have a lifetime longer than that of the function
> using it that declares it as nonlocal -- that function object may be
> deleted from the containing scope, but the variable of course still
> exists in the containing scope.

I actually changed my mind on that front when replying to Greg - I now
agree that visibility is the important aspect, but I also think there
are two levels of visibility within a function: local scope, which is
distinct for every call to the function, and "function scope", which
is shared across all invocations to the function. This difference is
reflected at the implementation level in where the references are
stored (i.e. as locals on the frame in the first case, as references
from the function object in the latter). Currently the only references
which live in that second scope without also being visible outside the
function's own lexical scope are default arguments, and those get
bound to ordinary local references when it comes to actually accessing
them from the body of the function.

Accordingly, even after conceding the visibility vs lifetime point,
I'm still in favour of extending nonlocal downwards to include
"function scope" in addition to its current usage for lexical scoping.
Finding a different word for "not as local as a local variable, but
not as nonlocal as a lexically scoped variable" has proven to be more
than a little challenging and the flat negation of "nonlocal" suggests
that any addition would be at least as ill fitting as "global" is at
the module end of the scale. Unfortunately, what I see as a minor
tweak to the meaning of nonlocal, yourself, Paul and Greg seem to see
as referring to a profoundly different concept. While that remains the
case, I doubt I'm going to have much luck persuading any of you as to
the reasonableness of the syntax.

I do find it interesting that at least some of the possible keywords
that have been suggested for the function scope use case were *also*
put forward as possibilities in the context of PEP 3104 (which
eventually gave us 'nonlocal') [1]. I'm not sure how much significance
to ascribe to that fact, but I do think it provides at least some
level of support for my thesis that the line between the two concepts
of function scoping and lexical scoping is blurrier than it first
appears.

Regards,
Nick.

[1] http://www.python.org/dev/peps/pep-3104/#id14

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



More information about the Python-ideas mailing list