クラス A (他の誰かからインポートしているので変更できない) をクラス X にインスタンス化しています。
A のメソッドへの呼び出しをインターセプトまたはラップする方法はありますか? つまり、以下のコードで呼び出すことができます
x.a.p1()
出力を取得します
X.pre
A.p1
X.post
ティアがいっぱい!
class A:
# in my real application, this is an imported class
# that I cannot modify
def p1(self): print 'A.p1'
class X:
def __init__(self):
self.a=A()
def pre(self): print 'X.pre'
def post(self): print 'X.post'
x=X()
x.a.p1()