469,927 Members | 1,674 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Sharing Base Class members

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 gr************@novasyshealth.com

"We are the music makers, and we are the dreamers of dreams" W.W.
Jul 18 '05 #1
1 1677
Greg Lindstrom wrote:
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Bryan Parkoff | last post: by
10 posts views Thread by Abelardo Vacca | last post: by
7 posts views Thread by relient | last post: by
4 posts views Thread by Francis Reed | last post: by
19 posts views Thread by jan.loucka | last post: by
15 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.