Reputation: 513
I can run API.py, but not the APP.py in the same directory. The error is "NameError: name 'a' is not defined". Is global() really global?
API.py:
class Entity():
def __init__(self,name,value):
globals()[name]=value
if __name__ == '__main__':
Entity('a',1)
print(a)
APP.py
from API import Entity
if __name__ == '__main__':
Entity('a',1)
print(a)
Upvotes: 1
Views: 96
Reputation: 484
Globals are only accessible by a every function in a module, but not by functions in other imported modules.
Instead of having Entity setting a global variable directly, perhaps you could have it return the value and then write code in APP.py to set the value globally.
The alternative is to have your global variable in a shared module that is imported by everyone else, and then every module will have read/write access to it.
Upvotes: 1