I have a python module with a lot of variables. These variables are used and changed by many other modules. And I want to reload this module from itself when it happens (for refresh).
How to do that?
upd: so.. this is me, question author, 8 years after the question publication date. I have zero idea why I would try to do something like that then.
If you read this, please attempt to use proper configuration management techniques (most of the time supplied by your framework) instead.
# ==================================
# foo.py
spam = 100
def set_spam(value):
spam = value
foo = reload(foo) # reload module from itself
# ==================================
# bar.py
import foo
print foo.spam # I expect 100
# assume that value changes here from some another modules (for example, from eggs.py, apple.py and fruit.py)
foo.set_spam(200)
print foo.spam # I expect 200
This operation doesn’t make sens at all. From Python documentation:
So,
reloadmake sens only from context of other module which performedimportpreviously.