"private" variables a.k.a. name mangling (WAS: What is print? A function?)
Steven Bethard
steven.bethard at gmail.com
Mon Jan 24 12:56:40 EST 2005
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
More information about the Python-list
mailing list