[Tutor] Learning about callbaks
alan.gauld at btinternet.com
Wed Jan 2 14:25:28 CET 2008
"Michael Bernhard Arp Sørensen" <michaelarpsorensen at stevnstrup.dk>
> I did some research, reading and test last night and I finally got
Sorry, but you didn't! However you are very nearly there...
def test_callback(self, this_callback):
print "testing the callback"
To actually use the callback you need to use parens:
But this won't work because of the problem below...
self.ui = UserInput()
print "hello world"
Here you do not pass the function object to your test_callback
you actually call it here! You bneed to pass the function as an object
then call it in the receiver
self.ui.test_callback(self, self.hello) # no parens means treat as
What you have done is executed the function(which prints the message
thus leading you to think it has worked) and passes the return
to your test_callback. But since you never actually call the function
(missing parens) there is no error message.
You can prove this by inserting a raw_input statement into
your test_callback before you use the callback. That way the
message should only appear after you hit return...
> I wanted to understand how a "parent" object could send a callback
> to a "child" object, and now I got it.
Nearly. You apply the parens when you want to execute the function
you omit parens when you want to treat the function as an object.
You need to swap your use of parens.
Author of the Learn to Program web site
More information about the Tutor