<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16441" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Within an application I'm working on. The app
is written in multiple layers such that lower layers provided services to higher
layers. Ideally in such an architecture, the high-level objects know about
lower-level ones, but lower-level objects know nothing about the higher-level
ones. There's only one problem. When this software was originally
wirtten, one of the low-level objects was given knowledge of a higher-level
object. This creates a really ugly dependency that I want to
eliminate.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>My solution (at least what I'm trying to implement)
is a classic one. When a low-level routine needs info from a higher-level
routine, let the higher-level routine provide a callback which the lower-level
routine can call. In this way, the lower-level routine knows nothing about
higher-level routines.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>However, Python is complaining about my
implementation. It raises an exception: TypeError: unbound method
fn_impl() must be called with X instance as first argument (got int instance
instead)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>For simplicity, I've narrowed it down to a bit of
sample code. class X is my low-level service.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>class X( object ):</FONT></DIV>
<DIV><FONT face=Arial size=2> fn = None</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> @staticmethod</FONT></DIV>
<DIV><FONT face=Arial size=2> def callX( n ):</FONT></DIV>
<DIV><FONT face=Arial size=2> return X.fn( n
)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Now, the following global stuff represents my
higher-level routines:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>def fn_impl( n ): # my
callback</FONT></DIV>
<DIV><FONT face=Arial size=2> return n + 1</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>X.fn = fn_impl #
register my callback</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Now I can do something which forces my callback
(fn_impl) to get called</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>print X.callX( 3 )</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I think I would get '4' printed but instead get the
above error. What am I doing wrong?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thanks,</FONT></DIV>
<DIV><FONT face=Arial size=2>Ron</FONT></DIV></BODY></HTML>