Functional programming gotcha

Gregor Lingl glingl at aon.at
Fri Oct 24 15:54:14 EDT 2003



David C. Fox schrieb:
....

> 
> I guess the nested scopes changes in Python 2.1/2.2 (see 
> http://www.python.org/peps/pep-0227.html) apply to functions but not 
> loops.  You can use the same workaround that people used to use because 
> of the lack of nested scopes:
> 
> f = lambda x, i = i: x + i
> 
> That way, the second, optional parameter has a default value which is 
> evaluated at the time the lambda is created.
> 

Isn't this the intended behaviour of nested scopes (regardless
of loops or not)?

 >>> def fun():
	i = 1
	def f():
		print i
	f()
	i+=1
	f()

	
 >>> fun()
1
2
 >>>

Gregor





More information about the Python-list mailing list