【发布时间】:2015-03-26 22:50:28
【问题描述】:
有人能解释一下它如何与 Python 解释器一起工作的逻辑吗?这种行为只是线程本地的吗?为什么第一个模块导入中的分配在第二个模块导入后仍然存在?我刚刚进行了一个很长的调试会话。
external_library.py
def the_best():
print "The best!"
modify_external_library.py
import external_library
def the_best_2():
print "The best 2!"
external_library.the_best = the_best_2
main.py
import modify_external_library
import external_library
external_library.the_best()
测试:
$ python main.py
The best 2!
【问题讨论】:
标签: python global-variables monkeypatching python-internals side-effects