Q: Python 2.0 preliminary features?

skaller skaller at maxtal.com.au
Sun Oct 17 09:14:00 EDT 1999


Jeremy Hylton wrote:
> 
> >>>>> "GE" == Greg Ewing <greg.ewing at compaq.com> writes:
> 
>   GE> I suspect that, if you introduce read-only lexical scoping,
>   GE> you're just going to substitute the stream of "Why is there no
>   GE> #@%$#$% lexical scoping in Python" complaints with "Why the
>   GE> #@%$#$% can't I assign to a non-local variable in Python"...
> 
> I agree whole-heartedly with you here!

	So does Guido, which is why he's (correctly)
resisted adding lexical scoping to Python, since it
doesn't permit rebinding variables out of a function
context (except with a hack, namely the global directive).

	This is a consequence of a design decision
to keep Python simple: no special declaration syntax,
for example. If you had to declare all variables before
use, the problem would go away.

	There is more than one solution to this.
One of them is to follow the usual rule in python:
if you need more than two scopes, the third one
has to be named, like 'self' in class methods.
This can be applied to activation records:

	def f(x):
		def g(y):
			super.x = 1

assigns to f's x by using the special keyword
'super' to refer to the parent. This idea
gets rid of 'global' declarations too:

	def f(x):
		super.glb = x  # set module variable


In fact, right now:

	def f(x):
		f.__module__['glb']  = x

would work, it's just messy. The same applies
to nested functions, which must track their
parents. All we need is a more convenient
syntax for existing functionality and we can
get rid of 'global' declarations, and solve
the syntactic problem for nested functions.

-- 
John Skaller, mailto:skaller at maxtal.com.au
1/10 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
downloads: http://www.triode.net.au/~skaller




More information about the Python-list mailing list