Python scope is too complicated

Ivan Van Laningham ivanlan at
Sun Mar 20 23:01:19 CET 2005

Hi All--

Dan Bishop wrote:
> > """In Python, there are only two scopes.  The global and the local.
> > The global scope is a dictionary while the local, in the case of a
> > function is extremely fast.  There are no other scopes.
> This isn't true anymore, now that generator comprehensions have been
> added to the language.
> >>> x = 17
> >>> sum(x for x in xrange(101))
> 5050
> >>> x
> 17

The equivalent in list comprehensions which currently allows the x to
leak out into its containing scope is going away soon.  Will that be
another scope?  Or are generator and list comprehensions only one scope?

Ivan Van Laningham
God N Locomotive Works
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours

More information about the Python-list mailing list