473,625 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Protecting instance variables

Hello,

I am really surprised that I am asking this question on the mailing
list, but I really couldn't find it on python.org/doc.

Why is there no proper way to protect an instance variable from access
in derived classes?

I can perfectly understand the philosophy behind not protecting them
from access in external code ("protection by convention"), but isn't
it a major design flaw that when designing a derived class I first
have to study the base classes source code? Otherwise I may always
accidentally overwrite an instance variable used by the base class...
Best,

-Nikolaus

--
»It is not worth an intelligent man's time to be in the majority.
By definition, there are already enough people to do that.«
-J.H. Hardy

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

Jul 18 '08 #1
2 1773
Nikolaus Rath schrieb:
Hello,

I am really surprised that I am asking this question on the mailing
list, but I really couldn't find it on python.org/doc.

Why is there no proper way to protect an instance variable from access
in derived classes?

I can perfectly understand the philosophy behind not protecting them
from access in external code ("protection by convention"), but isn't
it a major design flaw that when designing a derived class I first
have to study the base classes source code? Otherwise I may always
accidentally overwrite an instance variable used by the base class...

Here we go again...

http://groups.google.com/group/comp....467d724b48b32/

To directly answer your question: that's what the __ (double underscore)
name mangling is for.

Diez
Jul 18 '08 #2
Hi,

Sorry for replying so late. Your MUA apparently messes up the
References:, so I saw you reply only now and by coincidence.

"Diez B. Roggisch" <de***@nospam.w eb.dewrites:
Nikolaus Rath schrieb:
>Hello,

I am really surprised that I am asking this question on the mailing
list, but I really couldn't find it on python.org/doc.

Why is there no proper way to protect an instance variable from access
in derived classes?

I can perfectly understand the philosophy behind not protecting them
from access in external code ("protection by convention"), but isn't
it a major design flaw that when designing a derived class I first
have to study the base classes source code? Otherwise I may always
accidentally overwrite an instance variable used by the base class...

Here we go again...

http://groups.google.com/group/comp....467d724b48b32/

To directly answer your question: that's what the __ (double
underscore) name mangling is for.

I understand that it is desirable not to completely hide instance
variables. But it seems silly to me that I should generally prefix
almost all my instance variables with two underscores.

I am not so much concerned about data hiding, but about not
accidentally overwriting a variable of the class I'm inheriting from.
And, unless I misunderstood something, this is only possible if I'm
prefixing them with __.

How is this problem solved in practice? I probably don't have a
representative sample, but in the libraries that I have been using so
far, there were a lot of undocumented (in the sense of: not being part
of the public API) instance variables not prefixed with __. I have
therefore started to first grep the source of all base classes
whenever I introduce a new variable in my derived class. Is that
really the way it's supposed to be? What if one of the base classes
introduces a new variable at a later point?
Best,

-Nikolaus

--
»It is not worth an intelligent man's time to be in the majority.
By definition, there are already enough people to do that.«
-J.H. Hardy

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

Jul 28 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
5039
by: Yang Li Ke | last post by:
Hi guys! Anyone know a way so that users purchasing my scripts would not be able to share them with other people ? Yang
3
2514
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to explain instance variables. What I'm looking for is the best compromise between brevity and a full explanation. The students are non-CIS technical professionals ( engineers and scientists ). At the point they need this explanation, they have...
2
1771
by: Earl Teigrob | last post by:
I am programming ASP.NET using C#. I have been accessing static variables accross my entire application but now need to change some of the static variables that are session specific to instance variables. (For more background, see previous post about 30 min ago) It was so cool using static variables because they where global to the entire application so I could calculate them when the page loaded and use then anywhere in the app. Now...
3
1919
by: Claire | last post by:
I was ok understanding threads in Delphi pre .net, but reading through a reference book and comparing it to what help file says has confused me A System.Threading.Timer thread in one class calls an event in a 2nd class as follows (fast timer) public void OnDriverSendResult(rackdetail RackDetail) { // Add new RackDetail to linked list and leave quickly RackDetail.Previous = this.LastDetail;
6
1327
by: Roman Werpachowski | last post by:
In a recent thread http://tinyurl.com/8n7fe I asked about preventing the user from deleting the object pointed to by a pointer/reference. Now I would like to ask about a different aspect of this thing: it this protection worth it? It is fairly obvious that deleting an object you will need in the future is wrong. So is it worth to bother with protecting it against deletion?
1
1318
by: darrel | last post by:
I need to be able to password protect individual pages. For instance: /protected.aspx?id=123 /protected.aspx?id=555 Both would need to be only accessible to two different people (with their own usr/pwd). What is the most appropriate way to handle this?
12
1586
by: David | last post by:
Hi, I want to be able to make a class only visible within the namespace, i.e. only other classes within the namespace will have access to it. I have tried putting a protected keyword against it. That doesn't work. I have tried nesting it inside the class where I want to use it, this sort of compiles, except some of my methods that rely on the class have "Inconsistent accessibility:"
7
1499
by: JonathanB | last post by:
Ok, I know there has to be a way to do this, but my google-fu fails me (once more). I have a class with instance variables (or should they be called attributes, I'm newish to programming and get really confused with OO sometimes), like the one in this example: class Foo(): self.a = "bar" self.z = "test" self.var = 2
16
4195
by: rogerjames1 | last post by:
How would I go about protecting a whole directory, e.g. http://www.example.com/members/ and all sub-directories with login protection? I wouldn't like to put a .php script in each directory and I'd like to protect all file-types
0
8251
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8494
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2614
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 we have to send another system
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.