[BangPypers] Does Python have lexical scoping? (Noufal Ibrahim KV)
aditya.athalye at gmail.com
Sun Nov 9 19:11:22 CET 2014
Ref: This mail thread:
> Date: Sun, 09 Nov 2014 14:44:26 +0530
> From: Noufal Ibrahim KV <noufal at nibrahim.net.in>
> Subject: Re: [BangPypers] Does Python have lexical scoping?
> Message-ID: <87y4rkpwgd.fsf at nibrahim.net.in>
> [ ... snip ...]
> How is lexical scoping with a mutable environment different from
> dynamic scoping?
Here's my 0.0002 BTC; trying to reason from first principles:
In SML (and perhaps all strongly "functional" PLs), all values are
immutable by default.
Now, any function in SML is just a value (that can be passed around).
This value is, semantically, a "closure" that consists of (A) the
function definition and (B) the environment that was current at the
time it was defined. This closure, though somewhat
"special"--semantically a pair of (A) and (B)--is really designed to
be just a value.
If the PL is to guarantee the immutability of this function closure,
then _both_ (A) and (B) must be guaranteed immutable as well.
Re: Python semantics:
I'm not terribly familiar with Python semantics. But I think if
values/variables are mutable by default, then the function closure's
"contract" differs from to SML exactly the way Noufal describes it...
i.e. the closure _cannot_ close over values, it can only close over
variables defined within the lexical scope of a function.
Contrast with a mutable SML variant:
I think if I defined 'x' as a mutable ref in SML and reassigned some
other value to it (set-bang), I can violate the function closure
Re: Dynamic scope:
AFAIK, this goes one step beyond (behind?) mutable variables. Even if
*we* don't mutate the value of symbol 'x', another process is free to
inject some identically named symbol 'x' into our current environment,
just in time to mess with the wiring of an otherwise sane function.
Hopefully I'm making sense.
More information about the BangPypers