The problem is a need to take the arguments into account before choosing the responder. Here is my attempt so far.
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)
The desired effect would be:
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()
I will not know ahead of time all the functions contained with the responders A, B, and C.
Try this:
which() randomly selects an option from the choices, and which getattr uses to get the attribute.
EDIT: it actually looks like you want something more like this.
This could be written using lambda, but I’ll just leave it like this so it’s clearer.