Why read-only nested scopes?

Martin v. Loewis martin at v.loewis.de
Thu Sep 5 14:46:44 EDT 2002

bokr at oz.net (Bengt Richter) writes:

> How about introducing an optional "degree of globalness" for the
> global declaration? E.g.,

>     x = 1
>     def foo():
>       x = 2
>       def bar():
>         x = 3
>         def baz():
>           global(1) x
>           x = 4
> would go outwards 1 nested scope and thus target the x in bar,
> rebinding it from 3 to 4.

That would work - although I'd claim that some users will find that
notation plain ugly.

It will also have confusing results:

x = 1
def foo():
  print x
  def bar():
    global(1) x
    x = 1

At the moment, the "print x" in foo prints the global x, since there
is no assignment to x, and hence there is no local variable foo. With
the global statement, you declare that there is a local variable
foo::x. As a result, since there is no assignment to foo::x, this
would raise an UnboundLocal error.


More information about the Python-list mailing list