[Python-Dev] replacing 'global'
Guido van Rossum
guido at python.org
Mon Oct 27 12:28:33 EST 2003
> if I understand 'global x in f' will introduce a local x in f even if there
> is none, for symmetry with global. Maybe this has already been answered
> (this thread is getting too long, and is this change scheduled for 2.4 or
> 3.0?) but
>
> x = 'global'
>
> def f():
> def init():
> global x in f
> x = 'in f'
> init()
> print x
>
> f()
>
> will this print 'global' or 'in f' ? I can argument both ways which is not
> a good thing.
The compiler does a full analysis so it will know that init() refers
to a cell for x in f's locals, and hence it will print 'in f'. For
the purposes of deciding which variables live where, the presence of
'global x in f' inside an inner function (whether or not there's a
matching assignment) is equivalent to the presence of an assignment to
x in f's body.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list