Why read-only nested scopes?

Bengt Richter bokr at oz.net
Wed Sep 4 17:17:24 EDT 2002


On Wed, 4 Sep 2002 19:46:49 GMT, Andrew Koenig <ark at research.att.com> wrote:

>Gonçalo> Anyway, I've been directed to PEP 227, cheerfully obeyed, and
>Gonçalo> from what I have understood having read-only nested scopes
>Gonçalo> was more of a design decision than something else. And while
>Gonçalo> I can live happily with the decision (some would even say
>Gonçalo> happier) there is something about the "You can look at it but
>Gonçalo> not touch it" attitude that I dislike ;-)
>
>You can do anything to the object that the object support.
>What you can't do is change the binding of the name.
>
OTOH, if "everything is an object," then the namespace of
an enclosing scope could also be an object, and as such
could support rebinding of names therein ;-)

The question would be how to spell such access.

E.g., "global x" is a way to say that x=123 will be short for
the effect globals()['x']=123 within a certain scope.

If you visualized nested scopes as a list of scopes, starting with
locals and ending with globals, you could refer to them by index, e.g.,
scopes[0] for local and scopes[-1] for global, and others in between, if any.

If you treated those spaces like attribute name spaces, you could write
a silly example like

    def foo():
        x=123
        def bar():
           scopes[1].x=456
        bar()
        return x

and expect foo() => 456

Regards,
Bengt Richter



More information about the Python-list mailing list