loop scope

David MacQuigg dmq at gain.com
Fri Mar 19 21:55:48 EST 2004


On 19 Mar 2004 12:52:08 -0800, danb_83 at yahoo.com (Dan Bishop) wrote:

>Josiah Carlson <jcarlson at nospam.uci.edu> wrote in message news:<c3d4r8$gho$2 at news.service.uci.edu>...
>> > One way to avoid this problem is to have an explicit scope-creating
>> > construct instead.
>> > 
>> > def foo(a):
>> >    scope outer:
>> >       a = 3
>> >       scope inner:
>> >          a = 4
>> >          print a            # prints 4
>> >          print outer.a      # prints 3
>> >          print foo.a        # prints the function parameter
>> >       print a               # inner.a is out of scope, so prints 3
>> >       foo.b = 5             # Creates a new function-scope variable.
>> >    print b                  # prints 5
>> 
>> Ick.  Thank god Python doesn't seem to be heading in this direction.
>
>I agree.  Just because this was my idea doesn't mean I have to like it ;-)
>
>I'm satified with the scope rules the way they are, except for list comprehensions.

There is one small change I would make.  Nested scopes should apply to
*any* nested code, including nested classes.  This would make the
rules much simpler and easy to teach.

-- Dave




More information about the Python-list mailing list