473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Which way to say 'private'?

There are 2 ways to indicate private members of classes, by prepending
1 or 2 underscore characters, the latter causing name mangling. My
question is: When would I use which kind to indicate privacy?

Thanks,
Daniel Klein
Member of the Dead Parrot Society
Jul 18 '05 #1
5 6772
Daniel Klein wrote:
There are 2 ways to indicate private members of classes, by prepending
1 or 2 underscore characters, the latter causing name mangling. My
question is: When would I use which kind to indicate privacy?


You would normally use a single underscore, which is an advisory
indication of privacy. You would use two underscores, with the
mangling they produce, when you need to ensure against any risk
of accidental conflict with other existing names in the same space.

For example, if your class injects for its own purposes attributes
in other unrelated objects, it might be quite prudent to use the
double-underscore syntax for the names of those 'alien' attributes,
otherwise name clashes are far too likely to occur.
Alex

Jul 18 '05 #2
"Daniel Klein" <da*****@aracnet.com> wrote in message
news:14********************************@4ax.com...
There are 2 ways to indicate private members of classes, by prepending
1 or 2 underscore characters, the latter causing name mangling. My
question is: When would I use which kind to indicate privacy?


Hi.
The single underscore (self._attribute) is the convention when you wish to
indicate privacy. Where by "indicate" I mean that you wish to convey to
readers of the code that they should not access this attribute directly.
They can, but you are telling them that it's not the best practice for your
API. The double underscore (self.__attribute) mangles the name (as you've
mentioned), so it adds an extra disincentive to using that attribute
directly (at the very least, using instance._ClassName__attribute makes for
unattractive code). So, the latter method is used when you would really,
really prefer that people not access a particular attribute directly
(Nothing is stopping them, of course, but the intention is pretty clear).
Also, when someone subclasses your class, it makes accessing the
"privatized" attribute a little more difficult.
class C1: .... def __init__(self, value=1):
.... self.__value = value
.... class C2(C1): .... def __init__(self):
.... C1.__init__(self)
.... c2 = C2()
dir(c2)

['_C1__value', '__doc__', '__init__', '__module__']
# ^^^^^^^^^^^
# Here's the "private" attribute
Personally, I use single underscore to denote "protected", and double
underscore to denote "private" (if I use them at all).

HTH
Sean

Jul 18 '05 #3
On Thu, 21 Aug 2003 11:42:02 -0400, "Sean Ross"
<sr***@connectmail.carleton.ca> wrote:
"Daniel Klein" <da*****@aracnet.com> wrote in message
news:14********************************@4ax.com.. .
There are 2 ways to indicate private members of classes, by prepending
1 or 2 underscore characters, the latter causing name mangling. My
question is: When would I use which kind to indicate privacy?


Personally, I use single underscore to denote "protected", and double
underscore to denote "private" (if I use them at all).


Thanks for the courtesy or your reply, Sean.

I should probably have mentioned that I am concerned about advertising the
'public' interface (so that users of the class know how best to use it and
what my intentions were) more than 'restricting' access to 'private' members,
which we all know is pretty much pointless in Python ;-)

Thanks again,

Dan
Jul 18 '05 #4
Daniel Klein <da*****@aracnet.com> wrote in message news:<14********************************@4ax.com>. ..
There are 2 ways to indicate private members of classes, by prepending
1 or 2 underscore characters, the latter causing name mangling. My
question is: When would I use which kind to indicate privacy?

Thanks,
Daniel Klein
Member of the Dead Parrot Society


Well, one underscore is sort of a gentleman's agreement, it's like a
suggestion "you really should think before you touch this". Two
underscores is a stronger suggestion, like "I don't want you to touch
this and you'll have to go through hoops to do it".
Personally I always use two underscores for class-level data, and
write accessor methods.
One underscore is useful in a package-level declaration to prevent it
from being exported.
Hope this helps.
Jul 18 '05 #5
Daniel Klein <da*****@aracnet.com> writes:
On Thu, 21 Aug 2003 11:42:02 -0400, "Sean Ross"
<sr***@connectmail.carleton.ca> wrote:
"Daniel Klein" <da*****@aracnet.com> wrote in message
Personally, I use single underscore to denote "protected", and double
underscore to denote "private" (if I use them at all).


This is not the orthodox convention: single underscore indicates
"privacy" (more accurately: "this isn't part of the interface"):
double underscore is actually a mechanism for avioiding name clashes.
I should probably have mentioned that I am concerned about advertising the
'public' interface (so that users of the class know how best to use it and
what my intentions were) more than 'restricting' access to 'private' members,
which we all know is pretty much pointless in Python ;-)


Definitely single underscore.

(Also, remember that, with properties, you can have things which look
like direct attributes actulally be set and read with setter and
getter functions, so it is perfectly OK to make data attributes be
part of the interface, as you can later install getters and setters
for them without changing the interface.)
Jul 18 '05 #6

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

Similar topics

1
by: RWC | last post by:
Hi Folks, I'm looking for a way to determine if the client machine has access installed and if so, what version. The reason I need this is to determine (programatically) if the Access Runtime...
0
by: Kenneth Baltrinic | last post by:
I am getting the following error when deserializing an object that has a couple of dozen dependant objects in its object graph. Anyone who can suggest where I might begin to look to resolve problem...
3
by: nicver | last post by:
I am fixing a client's Web site and for some reason an ASP class does not want to use the variables it retrieves when it initialiases. This is an excerpt of the class and it is enough to show...
1
by: Sakharam Phapale | last post by:
Hi All, In following example, while playing file, if thread goes in WaitSleepJoin state, due to Thread.Sleep method. Now I want to suspend thread by clicking on cmdSuspend button. I have...
3
by: kiplring | last post by:
Suppose a function which has Sleep() method in it. And I want to recycle it. I made two buttons which call "Resume()" and "Suspend()". But It doesn't work. The state of thread "t" will be...
1
by: Pieter | last post by:
Hi, I have this Warning for several forms when using the DockPanel Suite of Weifen Luo in VS.NET 2005. I read a lot about this CLS-compliant stuff, but I don't now how to chech actually which...
3
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following...
2
by: Daniel Walzenbach | last post by:
Hi, I created an ASP.NET Datagrid where a single row can be selected by clicking anywhere on the row (according to...
114
by: Master Programmer | last post by:
Which do you prefer? Classic VB example: ************************* Private Sub Command1_Click() MsgBox "Hello, World" End Sub A VB.NET example: **************************
1
by: Martin | last post by:
Hi all ! I use Visual Studio .NET 2005 SP1 (+ Updates for Vista) on Windows Vista. I have a strange problem in a WinForm application. In a UserControl, which inherit another UserControl...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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,...

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.