KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Just confused about global value in Python, here are two piece of code #gl.py import cli a = 1 print "gl 1: %d %d" % (id(a), a) def reset(): global a a = 7 print "reset 1: %d %d" % (id(a), a) if __name__ == '__main__': cli.handler(reset) print "gl 2: %d %d" % (id(a), a) the cli code #cli.py def handler(func): from gl import a print "cli 1: %d %d" % (id(a), a) func() print "cli 2: %d %d" % (id(a), a) The result of execution is $ python gl.py gl 1: 150847672 1 gl 1: 150847672 1 cli 1: 150847672 1 reset 1: 150847600 7 cli 2: 150847672 1 #Why value doesn't change gl 2: 150847600 7 Here I don't understand after "function reset()" execution, the result of global value doesn't change in cli.py( cli 2: 150847672 1 ), but back to gl.py, global value does change!!
Tags (comma-separated)
Save Edits
Cancel