question about scope | | |
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?
And feel free to dissect that first sentence up above, because I just
don't get it.
Thanks. | | | | re: question about scope
John Salerno wrote:
[color=blue]
> 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'?[/color]
I guess maybe I jumped the gun. Here's some stuff later in the chapter
that I think explains it for me:
----------
Note that the second 'E' scope lookup layer -- enclosing defs or lambdas
-- technically can correspond to more than one lookup layer. It only
comes into play when you nest functions within functions.*
* The scope lookup rule was known as the "LGB" rule in the first edition
of this book. The enclosing def layer was added later in Python, to
obviate the task of passing in enclosing scope names explicitly --
something usually of marginal interest to Python beginners.
---------- | | | | re: question about scope
John Salerno schrieb:[color=blue]
> 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?
>[/color]
Nested functions, I should think. Absolutely silly, but working example:
[color=blue][color=green][color=darkred]
>>> def outer():[/color][/color][/color]
arg_in = 2
def inner():
x = arg_in * 3
return x
return inner()[color=blue][color=green][color=darkred]
>>> outer()[/color][/color][/color]
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@Bibliothek.Uni-Augsburg.DE | | | | re: question about scope
John Salerno said unto the world upon 16/02/06 09:18 AM:
<snip>
[color=blue]
> "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.[/color]
Does this help?
IDLE 1.1.2[color=blue][color=green][color=darkred]
>>> scope = "Global"
>>> def test():[/color][/color][/color]
scope = "enclosing"
def nested_test():
print scope
nested_test()
[color=blue][color=green][color=darkred]
>>> test()[/color][/color][/color]
enclosing[color=blue][color=green][color=darkred]
>>> def test2():[/color][/color][/color]
scope = "enclosing"
def nested_test():
scope = "nested"
print scope
nested_test()
[color=blue][color=green][color=darkred]
>>> test2()[/color][/color][/color]
nested[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Best,
Brian vdB | | | | re: question about scope
John Salerno wrote:
[color=blue]
> 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'?[/color]
consider a nested function:
var1 = "global"
def outer():
var2 = "enclosing"
def inner():
var3 = "local"
print var1, var2, var3
inner() # call it
inside "inner", var1 refers to the global variable, var2 to the enclosing
variable (which is local to "outer"), and var3 to the local variable.
"enclosing scope locals" are also called "free variables".
</F> | | | | re: question about scope
Fredrik Lundh wrote:[color=blue]
> John Salerno wrote:
>[color=green]
>> 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'?[/color]
>
> consider a nested function:
>
> var1 = "global"
>
> def outer():
>
> var2 = "enclosing"
>
> def inner():
>
> var3 = "local"
>
> print var1, var2, var3
>
> inner() # call it
>
> inside "inner", var1 refers to the global variable, var2 to the enclosing
> variable (which is local to "outer"), and var3 to the local variable.
>
> "enclosing scope locals" are also called "free variables".
>
> </F>
>
>
>[/color]
Thanks guys. It seems like nested functions were what the authors had in
mind, so that makes a lot more sense now.
But as far as ifs and loops, is there such a thing as scope in them? For
example, if I assign a variable within an if statement, is it usable
anywhere else? | | | | re: question about scope
John Salerno wrote:
[snip..][color=blue]
>
> Thanks guys. It seems like nested functions were what the authors had in
> mind, so that makes a lot more sense now.
>
> But as far as ifs and loops, is there such a thing as scope in them? For
> example, if I assign a variable within an if statement, is it usable
> anywhere else?[/color]
Defining a variable in a loop, or within an 'if block' doesn't change
scope, so the variables are useable in the same way as variables
outside the loop. Obviously if you define them within an if block, they
may *not* be defined, so using them could raise a NameError.
if False:
test = 'something'
print test
All the best,
Fuzzyman http://www.voidspace.org.uk/python/index.shtml | | | | re: question about scope
John Salerno wrote:[color=blue]
> 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.[/color]
second one
[color=blue]
> The whole flow of the
> sentence seems convoluted.[/color]
A bit, yes. In short: if a name is used in a function whithout having
been previously assigned in the body of this function, then this name is
looked up first in the enclosing functions definitions (python's
functions can be nested), then in the global (read : module) namespace,
then in the built-in namespace.
And if it's not found, it raises a NameError !-)
[color=blue]
> 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'?[/color]
This:
def outer_function():
a = "a in outer"
def inner_function():
print "in inner, a = %s" % a
inner_function()
Python functions can be nested (bis).
[color=blue]
>I thought that
> the only way to create a local namespace was if there *was* a function
> definition,[/color]
Yes. But since functions definitions can be nested...
[color=blue]
> 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?[/color]
I don't know.
[color=blue]
> Loops and if statements, perhaps?[/color]
Nope, AFAICT.
[color=blue]
> And feel free to dissect that first sentence up above, because I just
> don't get it.[/color]
Done.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xiludom.gro'.split('@')])" | | | | re: question about scope
On Thu, 16 Feb 2006 15:18:29 +0000, John Salerno wrote:
[color=blue]
> What's an example of a local scope without
> having a function definition? Loops and if statements, perhaps?[/color]
List comprehensions: [2*x+1 for x in range(50)]
Lambdas: map(lambda y: y-2, [1,2,4,8,16,32])
At the moment the x in list comprehensions are exported to the rest of the
local scope, but that (mis)feature is officially going to be removed in
future versions of Python.
--
Steven. | | | | re: question about scope
John Salerno wrote:
[color=blue][color=green]
> > 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'?[/color]
>
> I guess maybe I jumped the gun. Here's some stuff later in the chapter
> that I think explains it for me:
>
> ----------
> Note that the second 'E' scope lookup layer -- enclosing defs or lambdas
> -- technically can correspond to more than one lookup layer. It only
> comes into play when you nest functions within functions.*
>
> * The scope lookup rule was known as the "LGB" rule in the first edition
> of this book. The enclosing def layer was added later in Python, to
> obviate the task of passing in enclosing scope names explicitly --
> something usually of marginal interest to Python beginners.
> ----------[/color]
that footnote is slightly misleading, though -- there's no way to pass
in enclosing scope names in Python. what you can do is to pass in
*objects* into an inner scope, using the default argument syntax. an
example:
var = "global"
def func1(arg):
print var
def func2(arg, var=var):
print var
here, func1's "var" is bound to the *name* of the outer variable, so if
the variable is changed, it will print the new value.
in contrast, func2's "var" is bound to the *value* that the outer variable
had when the def statement was executed, so it will print the same thing
every time you call it, even if the outer variable is changed.
what the note refers to in modern python, the name binding (in func1)
also works as expected if you nest scope:
var = 0 # global
def outer():
var = 1 # local to outer, enclosing in func1
def func1(arg):
print var #enclosing in today's python, global in old pythons
def func2(arg, var=var):
print var
func1() # prints 1 in today's python, 0 in old pythons
func2() # prints 1 in all versions
var = 2
func1() # prints 2 in today's python, 0 in old pythons
func2() # prints 1
(I hope this didn't confuse things even more.)
</F> | | | | re: question about scope
John Salerno wrote:[color=blue]
> But as far as ifs and loops, is there such a thing as scope in them?[/color]
No.
Local scopes are introduced with "def" or "class", nothing
else (or did I forget something?). There is nothing in Python
that corresponds directly to the { } in C and C++. If you want
data to exist in some "other scope", put them in a dictionary
(there you have your { } ;^) and use that explicitly.
Each Python module/file is a global scope in Python.
Something to consider if you come from C, is that variables
and assignments in Python are conceptually different from C.
Your objects/values are never created in a local scope. They
are always created on the heap, as if you would use malloc in
C or new in C++. All variables are pointers/references, and
the objects/values are automatically garbage collected. From
a C perspective, the only kind of variables you have are void
pointers, but that's ok, because you can only point these
pointers to managed objects that are handled by the runtime
systems. The runtime system handles both memory allocation
and deallocation and type checks for you. In some particular
cases (e.g. numeric types) it will also perform casting when
you need it. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|