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 1874
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
Universitaetsbi bliothek, 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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 scope they meant public/protected/private access modifiers :)
Obviously all members of the base privately inherited class will be
private, and that was my answer. However, the teacher checked my
answers when I handed in, and the problem was that I had...
|
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 threads of a multithreaded
program using embedded SQL. Since the threads do not need to share
transaction scopes, the sqleAttachToCtx family of APIs do not seem to be
necessary.
<quote>
In the default implementation of threaded applications against...
|
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;
|
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;
|
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 shows the alert. What is the essential
reason?
function () { alert('hi mom'); }();
function () { alert('hi dad'); }(8);
var x=function () { alert('hi bro'); }();
| |
by: Cylix |
last post by:
I just know the form like:
myObject.prototype.linkFade = function(link, doShow) {
....
....
....
}
-----------------------------------------------------------------------------------------------------------------------------------
myObject.prototype.linkFade = function(link, doShow) { with (this) {
....
....
|
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 case a single object is created using object
literal notation and both the get and __Private methods are availabile
for use and no closure is created.
ABC.Util.SomeObject =
{
|
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 call
with "SomeObj".
Can someone describe why this is happening?
var SomeObj = {
doSomething : function()
{
|
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 global var
private void method1(int myInt){
int x; //local var
etc...
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |