I'm trying to do something along these lines:
class A:
def __init__( self, x=None, y=None ):
self.x = x
self.y = y
class B( A ):
def do_something_in_Bs_context( self ):
print "In B"
class C( A ):
def do_something_in_Cs_context( self ):
print "In C"
a = A(1,2)
b = B.do_something_in_Bs_context( a )
c = C.do_something_in_Cs_context( a )
As expected this code will generate this error:
TypeError: unbound method do_something_in_Bs_context() must be called with B instance as first argument (got A instance instead)
The underlying reason for this design is that A is a container of data ( say a table ) and B and C are a set of operations on A. Both B and C operate on the same data but are conceptually a distinct set of operations that can be performed on the same data. I could club all the operations in B and C inside A, but I want to create the separation in concepts. (As an example, on a table, I can perform different sets of operations which may be grouped into say Calculus or Trignometry. So A:Table, B: Calculus, C:Trignometry)
This reminded me of the Model-View-Controller paradigm with a slight twist.
I came up with the following solutions:
- B and C are implemented as conceptually different classes (a View/Controller) that maintain a reference to an instance of A and operate on that instance.
- Since B and C just group together methods on A, create modules with functions that operate on an instance of A.
I don't like either of these solutions much (2 slightly better than 1), but then I'm not sure if there is a better/cleaner/more pythonic way to solve this problem is. Any pointers?