別のモジュールの関数を別の関数に置き換えるのに問題があり、頭がおかしくなっています。
次のようなモジュール 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!
私は何を間違っていますか?