473,225 Members | 1,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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.
Feb 16 '06 #1
10 1832
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.
----------
Feb 16 '06 #2
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
Feb 16 '06 #3
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
Feb 16 '06 #4
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>

Feb 16 '06 #5
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?
Feb 16 '06 #6

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

Feb 16 '06 #7
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('@')])"
Feb 16 '06 #8
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.

Feb 16 '06 #9
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>

Feb 17 '06 #10
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.
Feb 22 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
6
by: Cylix | last post by:
I just know the form like: myObject.prototype.linkFade = function(link, doShow) { .... .... .... }...
4
by: emailscotta | last post by:
Below are some over simplified examples of code the create a single instance of an object and I was hoping some one could give me the pros and cons of each approach. First declaration: In this...
11
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.