いくつかの変数を作成するコードがあるとします。
# Some code
# Beginning of the block to memoize
a = foo()
b = bar()
...
c =
# End of the block to memoize
# ... some more code
ブロック内で作成/変更されたすべての変数について明示したり、手動でピクルしたりすることなく、上記のブロック全体をメモしたいと思います。Pythonでこれを行うにはどうすればよいですか?
理想的には、それを何か (if/else
またはwith
ステートメント) でラップし、必要に応じて強制的に更新するフラグを設定できるようにしたいと考えています。
概念的に言えば、次のようになります。
# Some code
# Flag that I can set from outside to save or force a reset of the chache
refresh_cache = True
if refresh_cache == False
load_cache_of_block()
else:
# Beginning of the block to memoize
a = foo()
b = bar()
...
c = stuff()
# End of the block to memoize
save_cache_of_block()
# ... some more code
コードで定義または変更された各変数を明示的にピクルする必要なく、これを行う方法はありますか? (つまり、最初の実行の最後に保存し、後で値を再利用するだけです)