関数名から関数へのマッピングを含む python ハッシュがあります。関連する関数を呼び出すように各ハッシュ エントリを変更したいのですが、最終的なカスタム関数も呼び出すようにします。出口フックのように振る舞うことです。
def original():
print "original work"
になる
def replacement():
original()
print "notify somebody..."
私の問題は、次のコードの出力が期待どおりではないため、スコープなどを間違っていると思います。多分私が尋ねることができれば、これを行うためのより良い方法はありますか? 元のcbの変更に固執したいのは、サードパーティのコードであり、変更する場所が少ないほど良いからです。
#!/usr/bin/python
def a():
print "a"
def b():
print "b"
def c():
print "c"
orig_fxn_cb = dict()
" basic name to function callback hash "
orig_fxn_cb['a'] = a
orig_fxn_cb['b'] = b
orig_fxn_cb['c'] = c
" for each call back routine in the hash append a final action to it "
def appendFxn(fxn_cb):
appended_fxn_cb_new = dict()
for i in orig_fxn_cb.keys():
cb = fxn_cb[i]
def fxn_tail():
cb()
print cb.__name__, "tail"
appended_fxn_cb_new[i] = fxn_tail
appended_fxn_cb_new[i]()
return appended_fxn_cb_new
" make up a modified callback hash "
xxx = appendFxn(orig_fxn_cb)
print xxx
for i in xxx:
print xxx[i]()