locals() and globals()

Paolo Pantaleo paolopantaleo at gmail.com
Sat Oct 14 10:27:23 EDT 2006


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



More information about the Python-list mailing list