Nested scopes: why is it weird?

Scott Long scott at swiftview.com
Sat Sep 8 00:11:18 CEST 2001


Paul Prescod wrote:

> 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".

Maybe get an error for the following?

def a():
  x = 0
  def b():
    x = 1

Something along the lines of "Local variable binding shadows variable
from an enclosing scope"? And then to force it, introduce 'local'
keyword:

def a():
  x = 0
  def b():
    local x # No really, I want my variable to be called 'x'
    x = 1



More information about the Python-list mailing list