プライベートでクラスの一部である関数にモンキー パッチを適用したいが、パッチを適用した関数も呼び出したい。
例:
class SomeClass:
def __some_function(self, foo):
return do_something()
今、私は次のようなものを書きたい
def new_function(self, foo)
if foo == 'bar':
return True
return super(self).__some_function(foo)
SomeClass.__some_function = new_function
これをデコレータで上書きしようとしましたが、アクセスできないため、古い関数を呼び出すのに問題がありました。また、モックライブラリをチェックしましたが、パラメータを指定して古い関数を呼び出す方法がわかりませんでした。
私が試したデコレータ:
def patch_something(method):
def new_function(self, foo):
if foo == 'bar':
return True
return method(self, foo)
return new_function
SomeClass.__some_function = patch_something(SomeClass.__some_function)
このエラーが発生します (クラスが別のファイルにあります - これは問題ですか?)。
AttributeError: type object 'SomeClass' has no attribute '__some_function'