[BangPypers] Does Python have lexical scoping?

Noufal Ibrahim KV noufal at nibrahim.net.in
Sun Nov 9 10:14:26 CET 2014


On Sun, Nov 09 2014, Abhishek L wrote:


[...]

> Here x was a mutable variable, doing a similiar ML construct, ie
>
>      val x = ref 2
>      fun f y = !x + 2
>
>      f 10 ; evals to 12
>      x := 10
>      f 10 ; evals to 20
>
> So this becomes a problem of closures over mutable variables? ie every
> closure looks up the value of same variable, x which gets mutated
> around. WDYT?

[...]

So the explanation I got is that it binds the environment at definition
time but the environment means a mutable table of key value pairs. In
other words, the closure closes only over the name of the variable and
not the value at definition time. I don't really understand how useful
this is unless you have some kind of environment that you can't get to
(e.g. non local inside a function etc.). Also, and this is my real
question,

How is lexical scoping with a mutable environment different from dynamic
scoping?



-- 
Cordially,
Noufal
http://nibrahim.net.in


More information about the BangPypers mailing list