Nested scopes: why is it weird?

Paul Prescod paulp at ActiveState.com
Fri Sep 7 17:20:29 EDT 2001


Cliff Wells wrote:
> 
>...
> > a = 5
> >
> > def foo():
> >    a = 6
> >
> > It would be better to require one of the names to be changed.
> 
> It silently does the right thing.  Changing the value of a variable from an
> enclosing scope is a _bad_ idea.  This is what is called a "side effect" and
> was one of the things structured programming was created to prevent.  

Side effects are a fundamental part of Python programming. If they
weren't, Python wouldn't have the "global" keyword. I think it is great
that Python requires you to be explicit when you want to overwrite a
global variable. I think it should similarly reqiure you to be explicit
when you want to shadow one. Or it could simply disallow the shadowing
-- it isn't very useful anyhow. Just call the inner a "inner_a".

-- 
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.ActiveState.com/pythoncookbook




More information about the Python-list mailing list