In Javascript, bind
, call
, and apply
allow you to change the reference to this
on demand. I'm looking at a situation in Python where the code would look a bit nicer if I were able to do this instead of using getattr
.
I'd like to pass a reference to a method, but change the instance the method refers to within a thread. In Javascript, I'd like to do this:
function produce (x) {
enqueue(this.api.delete.bind(this.api, x))
}
function consume () {
api = new API()
task = dequeue()
task.call(api)
}
In Python, I have to do this:
def produce(self, x):
enqueue(partial(self.api.delete, x))
def consume(self):
api = API()
task = dequeue() # callable
?????????
This may look a little odd, but what's going on is each api instance is running on a separate thread. getattr
works, but I would prefer to pass verbs instead of nouns to the queue.