473,398 Members | 2,368 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,398 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 1846
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;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.