question about scope

Brian van den Broek broek at cc.umanitoba.ca
Thu Feb 16 10:47:26 EST 2006


John Salerno said unto the world upon 16/02/06 09:18 AM:

<snip>

> "Name references search at most four scopes: local, then enclosing 
> functions (if any), then global, then built-in."
> 
> I understand what global and built-in are, and I thought I understood 
> the concept of local too, but when I got to this sentence (and the 
> previous sentence), I became confused about the first two scopes. What's 
> the difference between 'local' and 'enclosing functions'? I thought that 
> the only way to create a local namespace was if there *was* a function 
> definition, so now I'm confused by the apparent difference that the 
> authors are referring to. What's an example of a local scope without 
> having a function definition? Loops and if statements, perhaps?
> 
> And feel free to dissect that first sentence up above, because I just 
> don't get it.

Does this help?

IDLE 1.1.2
 >>> scope = "Global"
 >>> def test():
	scope = "enclosing"
	def nested_test():
		print scope
	nested_test()

	
 >>> test()
enclosing
 >>> def test2():
	scope = "enclosing"
	def nested_test():
		scope = "nested"
		print scope
	nested_test()

	
 >>> test2()
nested
 >>>

Best,

Brian vdB



More information about the Python-list mailing list