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.

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.

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)

For simplicity, I've narrowed it down to a bit of sample code.  class X is my 
low-level service.

class X( object ):
   fn = None

   @staticmethod
   def callX( n ):
      return X.fn( n )


Now, the following global stuff represents my higher-level routines:

def fn_impl( n ):   # my callback
   return n + 1

X.fn = fn_impl      # register my callback

Now I can do something which forces my callback (fn_impl) to get called

print X.callX( 3 )


I think I would get '4' printed but instead get the above error.  What am I 
doing wrong?

Thanks,
Ron
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to