loop scope

Jacek Generowicz jacek.generowicz at cern.ch
Mon Mar 15 09:54:23 CET 2004

David MacQuigg <dmq at gain.com> writes:

> >You can maintain that consistency by extending the list of
> >"boundaries" which define scopes; you already mentioned classes,
> >functions and modules ... now simply add list comprehensions (or
> >loops), and you will maintain perfect consistency of treatment of
> >local variables, without leaking variables out of list comprehensions
> >(or loops).
> I think the current design of Python is the right compromise between
> narrow scopes that avoid conflicts between identical names and broad
> scopes that minimize the need for global declarations.  Imagine having
> to declare globals inside every loop that needed to set some value
> outside of its own tiny little scope.

And what if the variable I want to affect is in an enclosing scope,
but not the global scope ?

Doesn't this rather suggest that the whole global declaration business
is rather flawed? It sort of made sense when we only had 3 scopes in
Python (only 2 of which were "userland" scopes) , but with nested
scopes it's simply not up to the job. Ultimately, the problem can be
traced to having no distinction between inctroducting a new binding
and rebinding an existing name.

More information about the Python-list mailing list