Calling member functions?
david_j_dawkins at spamless.hotmail.com
Thu Dec 13 19:40:29 CET 2001
"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
news:Xns9176803438443duncanrcpcouk at 127.0.0.1...
> "David Dawkins" <david_j_dawkins at spamless.hotmail.com> wrote in
> news:U60S7.9819$hg1.986922 at news6-win.server.ntlworld.com:
> > I can't quite get this right. How can I give an object instance and a
> > method as a callback for some other module/class to call at a later
> > point?
> Generally you dont. You just pass the method on its own:
> class Handler:
> def Callback(self):
> print "Callback called"
> class Notifier:
> def __init__(self, method):
> self.m_method = method
> def notify(self):
> h = Handler()
> n = Notifier(h.Callback)
> n.notify() # Calls h.Callback()
> h.Callback is a bound method. If you call it, then it calls the Callback
> method on the h instance. What you were doing was attempting to use it as
> though it were an unbound method. Calls to h.Callback take no arguments.
> Handler.Callback would be the unbound method. Calls to Handler.Callback
> take one argument. Your original code probably works if you pass in h and
> Handler.Callback, but usually it is cleaner to pass a single object for
> callback rather than two.
> A third option is to pass in h and the name of the callback method as a
> string and call it using getattr, but while this is sometimes useful it is
> generally messy.
OK, I see. It all makes sense now. It's like I curried the function with
Thanks for your help
More information about the Python-list