Hi.<br><br><div class="gmail_quote">On Jan 2, 2008 6:36 PM, Alan Gauld <<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Can you modify the program *without modifying the classes* to use an<br>ordinary function as the callback? Say this goodbye function:<br><br>def goodbye():<br> print "goodbye world"<br><br>This should not require more than 5 lines of new code and no changes
<br>to the<br>existing code. It could be done in 3...<br></blockquote></div><br>Like this?:<br><br><span style="font-family: courier new,monospace;">class UserInput:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
def __init__(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pass</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def test_callback(self, this_callback):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print "testing the callback"
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> this_callback()</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">class Game:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def __init__(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.ui = UserInput()</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
def hello(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print "hello world"</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def useUI(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.ui.test_callback(self.hello)</span>
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def goodbye():</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
print "goodbye world"</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">g = Game()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">g.useUI()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">g.ui.test_callback(goodbye)</span><br style="font-family: courier new,monospace;" clear="all">
<br>It took me a couple of minutes to understand your challenge. :-) Then I remembered that "ui" is instantiated inside "g" and therefore callable with the right parameter.<br><br>Thank you very, very much. I enjoy a good challenge.
<br><br>-- <br>Med venlig hilsen/Kind regards<br><br>Michael B. Arp Sørensen<br>Programmør / BOFH<br>I am /root and if you see me laughing you better have a backup.