473,414 Members | 1,716 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,414 software developers and data experts.

"private" variables a.k.a. name mangling (WAS: What is print? A function?)

Philippe C. Martin wrote:
class Debug_Stderr:
__m_text = ''
__m_log_text = None
__m_dbg = None
__m_refresh_count = 0


<rant>

I don't see the benefit in 99.9% of cases for making class variables
like this "private". If you don't want people to use them, simply use
the standard convention[1] for non-public variables:

class Debug_Stderr:
_text = ''
_log_text = None
_dbg = None
_refresh_count = 0

A lot of the time, it actually makes sense to make (at least some of)
the attributes public, e.g.:

class Debug_Stderr:
text = ''
log_text = None
dbg = None
refresh_count = 0

I don't know what all the variables in this specific example are
intended to be, but it certainly seems like something like
'refresh_count' might be useful to clients of the class.

Name mangling is there to keep you from accidentally hiding such an
attribute in a subclass, but how often is this really a danger? Can
someone give me an example of where __-mangling really solved a problem
for them, where a simple leading underscore wouldn't have solved the
same problem?

</rant>

Steve

[1] http://www.python.org/peps/pep-0008.html
Jul 18 '05 #1
5 1992
>Name mangling is there to keep you from accidentally hiding such an
attribute in a subclass, but how often is this really a danger? Can
someone give me an example of where __-mangling really solved a problemfor them, where a simple leading underscore wouldn't have solved the
same problem?


Look at the "autosuper" implementation on Guido's descrintro paper;
there the
fact that you user __super instead of _super is essential.

However I have written tens of thousands of lines of Python code and
never
needed __protected variables except in a few experimental scripts for
learning purpose. So I agree that the need does not occur often, but it
is still an useful thing to have.
Michele Simionato

Jul 18 '05 #2

[Steven]
Can someone give me an example of where __-mangling really solved a problem
for them, where a simple leading underscore wouldn't have solved the
same problem?


http://cvs.sourceforge.net/viewcvs.p...13&r2=1.13.4.1

That's a bugfix to SpamBayes, where I'd inadvertently named an instance
variable '_map' without realising that the base class
(asynchat.async_chat) also had an instance variable of that name. Using
double underscores fixed it, and had I used them from the beginning the
bug would never have cropped up (even if asynchat.async_chat had an
instance variable named '__map', which is the whole point (which you know,
Steven, but others might not)).

--
Richie Hindle
ri****@entrian.com

Jul 18 '05 #3
On Tuesday 25 January 2005 12:40, Richie Hindle wrote:

[Steven]
Can someone give me an example of where __-mangling really solved a problem for them, where a simple leading underscore wouldn't have solved the
same problem?

http://cvs.sourceforge.net/viewcvs.p...13&r2=1.13.4.1
That's a bugfix to SpamBayes, where I'd inadvertently named an instance
variable '_map' without realising that the base class
(asynchat.async_chat) also had an instance variable of that name. Using
double underscores fixed it, and had I used them from the beginning the
bug would never have cropped up (even if asynchat.async_chat had an
instance variable named '__map', which is the whole point (which you know,
Steven, but others might not)).


I have a counterexample. Consider refactoring a class from....

class B(A):
etc

.....into....

class C(A):
etc
class B(C):
etc

Usage of some double-undescore attributes moved from B to the new intermediate
base class C. Unit tests on B still passed, so that change is safe. right?

The problem occured because the double-underscore mangling uses the class
name, but ignores module names. A related project already had a class named C
derived from B (same name - different module). My refactoring caused
aliasing of some originally distinct double-underscore attributes.

--
Toby Dickenson

____________________

Important Notice:

This email and any attachments are confidential and may contain trade secrets or be legally privileged. If you have received this email in error you must not use, rely upon, disclose, copy or distribute the contents. Please reply to the sender so that proper delivery can be arranged and delete the email from your computer.
Gemini Data Loggers monitor incoming and outgoing email to ensure satisfactory customer service, maintain company security and prevent abuse of their email system. However, any views expressed in this email are not necessarily those of Gemini and Gemini cannot be held responsible for the content.
Gemini makes best efforts to ensure emails are virus free; however you are advised to carry out your own checks. Gemini does not accept responsibility for any damage resulting from email viruses.
____________________

Jul 18 '05 #4

[Toby]
The problem occured because the double-underscore mangling uses the class
name, but ignores module names. A related project already had a class named C
derived from B (same name - different module).


Yikes! A pretty bizarre case, but nasty when it hits. I guess the
double-underscore system can give you a false sense of security.

--
Richie Hindle
ri****@entrian.com

Jul 18 '05 #5
Toby Dickenson wrote:
I have a counterexample. Consider refactoring a class from....

class B(A):
etc

....into....

class C(A):
etc
class B(C):
etc

Usage of some double-undescore attributes moved from B to the new intermediate
base class C. Unit tests on B still passed, so that change is safe. right?

The problem occured because the double-underscore mangling uses the class
name, but ignores module names. A related project already had a class named C
derived from B (same name - different module). My refactoring caused
aliasing of some originally distinct double-underscore attributes.


Very interesting. I hadn't ever really thought about it, but I guess
this shows that even __-mangling won't solve all of the
attribute-renaming problems...

A somewhat related problem is briefly discussed in Guido's autosuper
example:
http://www.python.org/2.2.3/descrint...class_examples
where a base class derived from a class using autosuper but with the
same name as the superclass might get the wrong self.__super.

Steve
Jul 18 '05 #6

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

Similar topics

10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
1
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not available outside the class). I think I understand...
1
by: metiu | last post by:
Hi all, maybe it's an old question, but I couldn't find an answer... let's say I have this kind of directory structure for project foo: /foo/includes/mod1.h (exported by mod1)...
16
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I...
0
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
0
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: 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...

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.