[Python-Dev] Lexical scoping in Python 3k

Ka-Ping Yee python-dev at zesty.ca
Mon Jul 3 00:07:43 CEST 2006


On Sat, 1 Jul 2006, Andrew Koenig wrote:
> I'd rather see a simpler rule: = never defines a variable in a surrounding
> scope.  If you want to affect the binding of such a variable, you have to
> define it explicitly in the scope in which you want it.
>
> Example:
>
> 	x = 42
> 	def f():
> 		x = 123		# rebinds x as defined above
> 		y = 123		# defines local variable
> 	f()
> 	print x			# prints 123
> 	print y			# error -- y not defined
>
> Yes, I know that rule is too simplistic.  But I think I'd still prefer it to
> the way things are now.

I agree with you that this is a nicer and more consistent rule.
What do you think of the proposal for a keyword to say "don't rebind"?
It would achieve the same distinction you're aiming for above, but
without the drastic incompatibility with today's Python.

This has been previously discussed as "change the meaning of 'global'
to mean 'not local'":

    http://mail.python.org/pipermail/python-dev/2006-February/061568.html

    http://mail.python.org/pipermail/python-dev/2006-July/066908.html

I support this proposal, though i would prefer a clearer keyword
such as "outer x" or "nonlocal x".  If we can't agree on another
keyword (or can't afford to spend one more keyword), i'm willing
to support "global" for this purpose.


-- ?!ng


More information about the Python-Dev mailing list