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

Paul Moore p.f.moore at gmail.com
Tue Sep 27 22:15:28 CEST 2011

On 27 September 2011 20:53, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 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.

That description just makes my head hurt. To me, visibility is simply
which lines of code, when using a name, refer to that particular
variable. So a variable x at module level has "global scope" because
it is visible from all lines (in the module, ignoring shadowing
concerns). If x is defined within a function, it has local (to the
function) scope, because only lines within the function can see that
variable. In other words scope is essentially a lexical concept tied
to lexical visibility.

To that extent, these new closed-over values have local scope because
they are visible from the same lines of code as a local variable. But
their lifetime differs.

The ideas here are more commonly seen in the history of Lisp, which
originally had "dynamic" scope (which is more lifetime-oriented) but
moved to lexical (visibility-based) scope. There was a time when Lisps
were described as Lisp-1 or Lisp-2 depending on their position on

I need to go and do some research into old Lisp concepts to see how
they might apply here. I may be some time :-)


More information about the Python-ideas mailing list