Underscore data hiding (was python development practices?)

Martijn Faassen m.faassen at vet.uu.nl
Thu Nov 1 00:54:10 CET 2001

Russell E. Owen <owen at astrono.junkwashington.emu> wrote:
> This brings up an issue about using underscore to hide data...

> My understanding is that a double underscore is required for Python to 
> automagically mangle the name.

That is correct.

> On the other hand, two postings now 
> suggest that a single underscore is sufficient.

> If double underscore is required, then it seems to me this is a very 
> common misconception that a single is sufficient. It might be worth 
> changing.

The double underscore is required for name mangling, but I don't really
like the name mangling; it gets in the way. I just want to give the 
programmer a hint that some attribute is private, and I use a single
underscore for this, and many Python programmers with me.

Why does name mangling get in my way? Generally I like to be able to
reach the private variables in subclasses (I suppose this makes them
protected in C++ terms, though my C++ is rusty). I also like being
able to fake things easily in case I need it, for debugging and
testing purposes. In the end I only want to indicate something is private
without all the hassle of name mangling.

Data hiding isn't there to prevent evil programmers from doing evil
things anyway; it's there to communicate intent and to prevent people
from tripping over problems. At least in C++ they can do such evil things as 
#define private public before including a header file. Besides Python
has a tradition of flexibility and open source.

Finally, in Zope an underscore does have a meaning (it makes it private
in the Zope security sense).

> If double is not required, I'd love to know about it. Single looks a lot 
> neater, and I'm not fond of double punctuation symbols that are hard to 
> read anyway.

It's not required to make something be private in the intent sense. It's
only needed to make something private in the name mangling sense. :)


History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?

More information about the Python-list mailing list