loop scope

Jacek Generowicz jacek.generowicz at cern.ch
Fri Mar 12 17:13:19 CET 2004

David MacQuigg <dmq at gain.com> writes:

> On Fri, 12 Mar 2004 14:06:38 GMT, Arthur <ajsiegel at optonline.com>
> wrote:
> >On Thu, 11 Mar 2004 17:54:56 -0700, David MacQuigg <dmq at gain.com>
> >wrote:
> >>Scopes are defined by the boundaries of functions, classes and
> >>modules, not for loops.  This is essential, or you would have to
> >>declare global variables inside most for loops.

> The one exception I can think of is what Terry Reedy mentioned - the
> iteration variable in a list comprehension.  In some future
> optimization, they may neglect to save that variable.  I hope they
> don't do that (even though it really has no use).  I just like the
> consistency we now have in treatment of all local variables.

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

More information about the Python-list mailing list