Python で、インスタンス化されたときに任意のメソッド呼び出しを受け取ることができるクラスを作成できますか? 私はこれを読んだが、断片をまとめることができなかった
と何か関係があると思われますattribute lookup
。クラスの場合Foo
:
class Foo(object):
def bar(self, a):
print a
class 属性は で取得できますprint Foo.__dict__
。
{'__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__module__': '__main__', 'bar': <function bar at 0x7facd91dac80>, '__doc__': None}
したがって、このコードは有効です
foo = Foo()
foo.bar("xxx")
を呼び出すとfoo.someRandomMethod()
、AttributeError: 'Foo' object has no attribute 'someRandomMethod'
結果が得られます。
foo
オブジェクトがランダムな呼び出しを受け取り、デフォルトで何もしないようにしたい。
def func():
pass
どうすればこれを達成できますか?この動作で、テスト用のオブジェクトをモックしたいと考えています。