How do I set a callback in Python?
Gary Herron
gherron at islandtraining.com
Mon Sep 8 03:21:52 EDT 2008
catsclaw wrote:
> I can't for the life of me figure out how to set a callback in
> Python. I have a class, which wraps another class. The second class
> needs a callback assigned. I don't want to use globals for it.
> Here's what I'd like to do:
>
> class MyWrapper:
> def get_login(self, username):
> return self.user, self.pass
>
> def __init__(self, user, pass):
> self.user = user
> self.pass = pass
>
> self.client = Client("connection string")
> self.client.callback_login = get_login
>
Make that list line:
self.client.callback_login = self.get_login
Then you are passing a "bound" method (meaning bound to a particular instance).
The client class calls its callback_login as a normal function with parameters,
and the Wrapper get_login will be called on the proper instance of MyWrapper.
This is a very common thing for many uses, including callbacks.
Hoping I understood what you wanted correctly,
Gary Herron
> ... but obviously, the Client class, when it calls the callback,
> doesn't pass a reference to the "self" object. How do I do this?
>
> -- Chris
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list