loop scope

David MacQuigg dmq at gain.com
Fri Mar 12 12:40:29 EST 2004


On 12 Mar 2004 17:13:19 +0100, Jacek Generowicz
<jacek.generowicz at cern.ch> wrote:

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

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.

-- Dave




More information about the Python-list mailing list