Sharing Base Class members

Greg Lindstrom greg.lindstrom at novasyshealth.com
Mon Jul 12 10:27:30 EDT 2004


Hello-

I have a base class which does the heavy lifting associated with creating,
modifying, and writing record segments for an application I've developed.
One of the requirements of the app is I must track how many segments have
been written to the output stream.  Since the Write() method is in the base
class, I'd like to add a member in the base class, but I would like to
"share" it across all objects created with the base class. Suppose

class myBase:
    def __init__(self, op=None):
         self.op = op
         self.segment = 'This is a test'
    def Write(self):
          if self.op is not None:  op.Write(self.segment)

class A(myBase, op=None):
     def __init__(self, op=op):
         myBase.__init__(op=op)

class B(myBase, op=None):
     def __init__(self, op=op):
         myBase.__init__(op=op)

Can, or rather *how*, can I add a member to myBase that will increment each
time A.Write() or B.Write() is called?  I will then add a
TotalSegmentsWritten() method to myBase to get the total number of segments
written.

I'm using Python 2.3.3 on Windows XP "professional"

Thanks!
--greg

Greg Lindstrom                                         (501) 975-4859
NovaSys Health                  greg.lindstrom at novasyshealth.com

"We are the music makers, and we are the dreamers of dreams"  W.W.





More information about the Python-list mailing list