Python – Historical Reason for Double Underscore in Class Private Members

historypython

In python a module private function or variable is named _foo.
If it is private to a class it is names __foo,
The mechanics behind how these work are different, however that is of little relevance to the question.

Why, during the design of the Python language was the double underscore characters chosen for class private members?
Why not single underscore, like for module private?
Why not some other character that is more visible? (Differentiating between _ and __ is hard).

I'm sure there is some history here, but I'm having trouble finding it, because all I find are tutorials telling me what it means, rather than why it is that way.

Best Answer

Python Enhancement Proposal (PEP) 8 explains the intended meaning behind single- and double-underscore names, among other conventions. Like many of the early PEPs, it represents Guido van Rossum's preferences, and as Python's acknowledged Benevolent Dictator for Life, his preferences are generally followed by the Python community.

The use of double underscores to indicate "magic" words goes at least as far back as the C preprocessor, which used that convention starting in the 1970s (e.g., __FILE__, __LINE__). It did so because there were no symbols that were off-limits to the preprocessor, so a rarely-used naming pattern (multiple underscores and upper case words) virtually ensured a lack of conflicts.