473,471 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1785
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
4
by: Francis Reed | last post by:
Hi I'm currently developping a University portal that uses single sign on between multiple applications, and I would like to store my users in one application and share them with other...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.