問題是需要在選擇響應者之前考慮參數。這是我迄今的嘗試。需要在Python類中路由實例調用
from responders import A, B, C
class RandomResponder(object)
def init(self, *args, *kwargs):
self.args = args
self.kwargs = kwargs
def __getattr__(self, name):
# pick a responder based on the args the function was called with
# I don't know how to do this part
# for sake of argument lets the args a function was called with lead me to pick responder A
r = A
responder = r(*self.args, **self.kwargs)
return responder.__getattr__(name)
的預期效果是:
r = RandomResponder()
r.doSomething(1)
#returns A.doSomething()
r.doSomething(2)
#returns B.doSomething()
r.doSomething(3)
#return C.doSomething()
r.doSomethingElse(1)
#returns A.doSomethingElse()
r.doSomethingElse(2)
#returns B.doSomethingElse()
r.doSomethingElse(3)
#returns C.doSomethingElse()
我不知道提前包含與應答A,B和C
你很難清楚你想要做什麼,你能提供更多信息嗎? (我也會澄清標題) – 2009-01-26 02:01:38
請用更多的事實更新你的問題;評論一個答案不如解決你的問題。 – 2009-01-26 02:48:47