Why read-only nested scopes?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Wed Sep 4 15:29:25 EDT 2002

On 04 Sep 2002 20:38:30 +0200, loewis at informatik.hu-berlin.de (Martin v.
Löwis) wrote:

>Gonçalo Rodrigues <op73418 at mail.telepac.pt> writes:
>> >Gonçalo Rodrigues <op73418 at mail.telepac.pt> writes:
>> >
>> >> So why hasn't this been extended when nested scopes were introduced,
>> >> e.g. by reusing the global directive or some other one? 
>> >
>> >I think the main reason is that nobody could propose an acceptable
>> >syntax. Reusing the global directive does not work.
>> Can you explain why it does not work?
>x = 1
>def foo():
>  x = 2
>  def bar():
>    x = 3
>    def baz():
>      global x
>      x = 4
>If you invoke baz(), which of the variables is changed? How do you
>denote that you want to change the others?

I think that crawling up the scope hierarchy and stopping at the first x
that one finds (in the above case, the one defined in bar) is a sensible
solution. Any other solution just seems perfect madness, but then again,
most probably I am overseeing something.

Anyway, I've been directed to PEP 227, cheerfully obeyed, and from what
I have understood having read-only nested scopes was more of a design
decision than something else. And while I can live happily with the
decision (some would even say happier) there is something about the "You
can look at it but not touch it" attitude that I dislike ;-)


Thanks 4 everything and all the best,
Gonçalo Rodrigues

More information about the Python-list mailing list