question about scope

Sibylle Koczian Sibylle.Koczian at Bibliothek.Uni-Augsburg.de
Thu Feb 16 10:38:30 EST 2006


John Salerno schrieb:
> Here's a sentence from Learning Python:
> 
> "Names not assigned a value in the function definition are assumed to be
> enclosing scope locals (in an enclosing def), globals (in the enclosing
> module's namespace) or built-in (in the predefined __builtin__ names
> module Python provides."
> 
> I have trouble reading this sentence. First, I don't understand if the
> word 'enclosing' is a verb or an adjective. The whole flow of the
> sentence seems convoluted.
> 
> But my real question is this, which is related to the above:
> 
> "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?
> 

Nested functions, I should think. Absolutely silly, but working example:

>>> def outer():
	    arg_in = 2
	    def inner():
		    x = arg_in * 3
		    return x
	    return inner()
>>> outer()
6

Seen from the inside of "inner" x is local, arg_in is in the enclosing
function.



-- 
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Sibylle.Koczian at Bibliothek.Uni-Augsburg.DE



More information about the Python-list mailing list