Issue
I have three files:
globals.py:
value = None
reader.py:
from globals import *
def read_global():
print(value)
changer.py:
from globals import *
from reader import *
def change_global():
global value
value = 1
change_global()
read_global()
I would expect the call to "read_global" would print 1, but the value None is printed.
Why is that the case? Why the new value set in "change_global" doesn't print?
Solution
I've been reading about modules in the Python documentation and it states:
Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.
This implicates that the global symbol table is not shared between modules.
Answered By - luke1985 Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.