[Python-Dev] Re: closure semantics

Guido van Rossum guido at python.org
Thu Oct 23 13:09:05 EDT 2003


> Guido:
> > My problem with the nested functions is that it is much harder to get
> > a grasp of what the shared state is -- any local variable in the outer
> > function *could* be part of the shared state, and the only way to tell
> > for sure is by inspecting all the subfunctions.
> 
> That would be solved if, instead of marking variables
> in inner scopes that refer to outer scopes, it were
> the other way round, and variables in the outer scope
> were marked as being rebindable in inner scopes.

[Greg]
>   def f():
>     rebindable x
>     def inc_x_by(i):
>       x += i # rebinds outer x
>     x = 39
>     inc_x_by(3)
>     return x

This would only apply to *assignment* from inner scopes, not to *use*
from inner scopes, right?  (Otherwise it would be seriously backwards
incompatible.)

I'm not sure I like it much, because it gives outer scopes (some) control
over inner scopes.  One of the guidelines is that a name defined in an
inner scope should always shadow the same name in an outer scope, to
allow evolution of the outer scope without affecting local details of
inner scope.  (IOW if an inner function defines a local variable 'x',
the outer scope shouldn't be able to change that.)

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list