Sharing Base Class members

G

Greg Lindstrom

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 (e-mail address removed)

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

Peter Hansen

Greg said:
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:
segmentsWrittenCount = 0
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)
myBase.segmentsWrittenCount += 1
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)

These two __init__ methods, as written, are redundant. If you remove
them entirely, you'll get the same effect as what you have written
here (i.e. myBase.__init__ will be called with the op argument).
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.

See code inserted above. This is a "class variable" and as long
as you reference it with the class name instead of using "self."
you will get to that one single item defined in the base class.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top