472,353 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

locals() and globals()

Hi

this exaple:

def lcl():
n=1
x=locals()
x["n"]=100
print "n in lcl() is:" +str(n)
#This will say Name error
#x["new"]=1
#print new
n=1
x=globals()
x["n"]=100
print "gobal n is:" +str(n)
x["new"]=1
print "new is:" +str(new)
lcl()

produces

gobal n is:100
new is:1
n in lcl() is:1

shouldn't be n in lcl() 100 too?

why accessing the names dictionary globals() and locals() gives
different results?
This example was made using
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32

PAolo
Oct 14 '06 #1
1 1497
Paolo Pantaleo wrote:
Hi

this exaple:

def lcl():
n=1
x=locals()
x["n"]=100
print "n in lcl() is:" +str(n)
#This will say Name error
#x["new"]=1
#print new
n=1
x=globals()
x["n"]=100
print "gobal n is:" +str(n)
x["new"]=1
print "new is:" +str(new)
lcl()

produces

gobal n is:100
new is:1
n in lcl() is:1

shouldn't be n in lcl() 100 too?

why accessing the names dictionary globals() and locals() gives
different results?
Python is statically scoped. The name bindings are fixed at compile
time. Hence you can't achieve dynamic scoping by using the locals()
dictionary and add new names to the locals() which might eventually be
used as local variables in the body of the function. The function still
controls its own state and does not permit delegating state changes via
locals(). That's what objects are for.

Oct 14 '06 #2

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

Similar topics

2
by: Giles Brown | last post by:
I do not understand why the following code produces NameError: name 'FirstClass' is not defined when both a global and local dict are passed into...
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.