関数に副作用がないこと、または正確な変数に影響を与える副作用のみがあることを確認したいと思います。実際に副作用(または特定の変数のみの副作用)がないことを確認する機能はありますか?
そうでない場合は、どうすれば次のように自分で書くことができますか?
私の考えは、初期化して、テスト対象の関数を呼び出してから、finalメソッドを呼び出すようなものです。
class test_side_effects(parents_scope, exclude_variables=[]):
def __init__():
for variable_name, variable_initial in parents_scope.items():
if variable_name not in exclude_variables:
setattr(self, "test_"+variable_name, variable_initial)
def final(self, final_parents_scope):
for variable_name, variable_final in final_parents_scope.items():
if variable_name[:5] is "test_" and variable_name not in exclude_variables:
assert getattr(self, "test_"+variable_name) is variable_final, "Unexpected side effect of %s from %s to %s" % (variable_name, variable_initial, variable_final)
#here parents_scope should be inputted as dict(globals(),**locals())
これがまさに私が欲しい辞書かどうかはわかりませ ん...
最後に、私はこれを行うべきですか?そうでない場合は、なぜですか?