[Types-sig] type declaration syntax

skaller skaller@maxtal.com.au
Fri, 24 Dec 1999 13:01:27 +1100


Guido van Rossum wrote:
> 
> [John Skaller]
> > CPython does not lexically scope function bodies
> 
> I'm not sure what you mean by lexically scoped here, since in my
> opinion Python functions *are* lexically scoped -- however the scopes
> don't nest like they do in Pascal etc.  (The opposite of lexical
> scoping is dynamic scoping, which is emphatically *not* used in Python
> -- variables bound in outer stack frames don't affect a function's
> use of variable names.)
> 
> > default arguments _are_ lexically scoped.
> 
> I'm not sure what you mean here either.  Defaults are evaluated in the
> containing scope.  I'm not sure how that makes them any more lexically
> scoped.

I agree with what you say, your guess at what I meant is correct.
I should have given a more detailed description. The issue remains,
independently of the terminology used to describe it:

	def f(x):
		def g(a): ....
		return g

Here, each g created on invocation of f is has the same behaviour
on each invocation of f, no matter what the arguments of f are,
and no matter what the values of the locals of f are. But each
invocation of f() returns a _new_ function called g: a distinct
object.

This is not the case if g has a default argument:

	def f(x):
		def g(a=x): return a
		return g

	
	g1 = f(1)
	g2 = f(2)
	print g1(), g2()

prints 1,2: g1 and g2 have different behaviours, even though
the bodies of the functions are the same, because the
default arguments differ. So _part_ of a function definition
may depend on the definition context, namely, its default
arguments. Unlike C and C++, in Python default arguments
are part of the function.

-- 
John Skaller, mailto:skaller@maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850