How does this code works:

Robert Kern rkern at ucsd.edu
Mon Jul 11 19:06:16 EDT 2005


vch wrote:
> Here's an example from some book:
> 
> def foo(n):
> 	s = [n]
> 	def bar(i):
>   		s[0] += i
>   		return s[0]
>   	return bar
> 
> what I don't understand is how this example works, taking into account 
> the LGB rule. I thought that s is not accessible from bar, but it is, 
> apparently. Why?

Python now has nested scopes.

http://www.python.org/peps/pep-0227.html

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list