Nested scopes: why is it weird?

Scott Long scott at swiftview.com
Fri Sep 7 17:45:13 EDT 2001


Cliff Wells wrote:

> Note that _accessing_ a variable from an enclosing scope (or global, for that
> matter), isn't nearly as bad and again, Python does the correct thing: it
> allows you to see the variables from the enclosing scope, but the minute you
> try to assign to them, they become local to the current scope (even if you do
> the assignment _after_ you try to read the value - this IS confusing if you
> don't know why)

And that was my original confusion. As you stated, the only real reason
to have access to enclosing scopes is for reading outer-scope variables,
never for setting them. But it seems that if a nested function needs
access to an outer variable, you should just pass that variable as a
parameter.

It looks like the real reason for nested scopes was to get rid of the
lambda hack (passing variables through default arguments). Perhaps
nested scopes are over-general, and should ONLY apply to lambda
expressions? That's really the only place they are necessary...

Scott



More information about the Python-list mailing list