我在用另一个功能替换另一个模块中的功能时遇到麻烦,这让我发疯。
假设我有一个看起来像这样的模块bar.py:
from a_package.baz import do_something_expensive
def a_function():
print do_something_expensive()
我还有另一个看起来像这样的模块:
from bar import a_function
a_function()
from a_package.baz import do_something_expensive
do_something_expensive = lambda: 'Something really cheap.'
a_function()
import a_package.baz
a_package.baz.do_something_expensive = lambda: 'Something really cheap.'
a_function()
我希望得到结果:
Something expensive!
Something really cheap.
Something really cheap.
但是我得到了这个:
Something expensive!
Something expensive!
Something expensive!
我究竟做错了什么?