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. 10 1738
John Salerno wrote: 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 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.
----------
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 : Si*************@Bibliothek.Uni-Augsburg.DE
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
John Salerno wrote: 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'?
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>
Fredrik Lundh wrote: John Salerno wrote:
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'?
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>
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?
John Salerno wrote:
[snip..] 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?
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
John Salerno wrote: 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.
second one
The whole flow of the sentence seems convoluted.
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 !-)
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'?
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).
I thought that the only way to create a local namespace was if there *was* a function definition,
Yes. But since functions definitions can be nested...
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?
I don't know.
Loops and if statements, perhaps?
Nope, AFAICT.
And feel free to dissect that first sentence up above, because I just don't get it.
Done.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
On Thu, 16 Feb 2006 15:18:29 +0000, John Salerno wrote: What's an example of a local scope without having a function definition? Loops and if statements, perhaps?
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.
John Salerno wrote: 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 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. ----------
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>
John Salerno wrote: But as far as ifs and loops, is there such a thing as scope in them?
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. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
8 posts
views
Thread by __PPS__ |
last post: by
|
11 posts
views
Thread by Mark Yudkin |
last post: by
|
9 posts
views
Thread by Stefan Turalski \(stic\) |
last post: by
|
53 posts
views
Thread by Jeff |
last post: by
|
7 posts
views
Thread by Csaba Gabor |
last post: by
|
6 posts
views
Thread by Cylix |
last post: by
|
4 posts
views
Thread by emailscotta |
last post: by
|
11 posts
views
Thread by emailscotta |
last post: by
|
20 posts
views
Thread by David |
last post: by
|
5 posts
views
Thread by somenath |
last post: by
| | | | | | | | | | |