[Python-Dev] 2.5 and beyond

Andrew Koenig ark at acm.org
Sat Jul 1 01:04:53 CEST 2006


> That's not a very constructive proposal (especially since I don't know
> Scheme). Perhaps you could elaborate on what needs to change?

The fundamental principle is that the binding of every name is determined
during compilation, not during execution.  This property does not quite
apply to Python at present.  For example:

	x = 42
	def f():
		y = x
		x = 123
		return y
	f()

This example fails with "local variable 'x' referenced before assignment"
because the compiler sees that f contains an assignment to x, so it makes x
a local variable, and then when you try to assign x to y during execution,
it fails.  This behavior is consistent with the notion of lexical scoping.

However, if I write

	def g():
		return x
	x = 42
	g()

the result is 42.  With lexical scoping, I believe it should be undefined.

The reason is that when the compiler encounters the definition of g,
variable x is not yet bound, and there is nothing in the body of g that
would bind it.  Therefore, g requires a binding to exist at the time it is
compiled; because no such binding exists, this example would be an error (at
compile time) under lexical scoping.








More information about the Python-Dev mailing list