Hi.<br><br><div class="gmail_quote">On Jan 2, 2008 6:36 PM, Alan Gauld &lt;<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>&gt; 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> &nbsp; &nbsp; print &quot;goodbye world&quot;<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;">
&nbsp;&nbsp;&nbsp; def __init__(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; def test_callback(self, this_callback):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;testing the callback&quot;
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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;">&nbsp;&nbsp;&nbsp; def __init__(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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;">
&nbsp;&nbsp;&nbsp; def hello(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;hello world&quot;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; def useUI(self):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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;">
&nbsp;&nbsp;&nbsp; print &quot;goodbye world&quot;</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 &quot;ui&quot; is instantiated inside &quot;g&quot; 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.