globals() and locals() : Variable Scope « Function « Python Tutorial






def foo():
    print '\ncalling foo()...'
    aString = 'bar'
    anInt = 42
    print "foo()'s globals:", globals().keys()
    print "foo()'s locals:", locals().keys()

print "__main__'s globals:", globals().keys()
print "__main__'s locals:", locals().keys()








10.4.Variable Scope
10.4.1.Variable Scope and Namespaces
10.4.2.Shadow variable
10.4.3.globals() and locals()
10.4.4.create an instance to use the class simply as a namespace container
10.4.5.Variable name lookup in nested function
10.4.6.Local Names Are Detected Statically
10.4.7.Variable Scope in Functions