473,573 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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_cou nt = 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 2005
>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
3307
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 understanding to what extent I should follow this policy in my Python code so I thought I'd ask the group. I read from a previous post that to...
1
23154
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 Shared (available without having to instantiate a class). So how could a Private Shared Function be called? Why give it this particular scope?
1
1656
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) /foo/includes/mod2.h (exported by mod2) /foo/mod1/file1.c /foo/mod1/file1.h (exported by file1)
16
2034
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 frequently use this for such things as caching, or for keeping track of how many objects a factory function has created, and so on. Today it...
0
3993
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 parse that sentence, btw?) in favour of using encapsulation. Of the top of my head, there is Java, C# and Python, and I think even PHP and Perl....
0
7781
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...
0
7699
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...
0
8202
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7788
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6421
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...
0
3733
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...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
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
0
1041
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...

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.