What does the double underscore indicate in python? I remember reading a tutorial that said it had something to do with a hidden variable, but I feel like there is more to it than that and I keep seeing examples of code that have double underscores and I don't understand what it means.
Asked
Active
Viewed 9,308 times
8
-
Depends where the double underscores are used. (`__init__`? `__name__`?) – David Robinson Oct 07 '13 at 04:08
-
I am asking strictly about double underscores before the name, not before and after. – david Oct 07 '13 at 04:10
-
1@donnaloia Then it is indeed a duplicate. – Lennart Regebro Oct 07 '13 at 04:12
-
1Are the people marking this as a duplicate also giving it downvotes? That seems unfair. – Mark Ransom Oct 07 '13 at 04:42
-
Not me. In fact, I upvoted the question :) – paulsm4 Oct 07 '13 at 05:30
-
I wish someone would explain this in layman's terms, even reading all the links posted, it's still confusing to me. – david Oct 07 '13 at 05:47
1 Answers
8
From PEP 8:
__double_leading_underscore
: when naming a class attribute, invokes name mangling (inside class FooBar,__boo
becomes_FooBar__boo
; see below).__double_leading_and_trailing_underscore__
: "magic" objects or attributes that live in user-controlled namespaces. E.g.__init__
,__import__
or__file__
. Never invent such names; only use them as documented.

Mark Ransom
- 299,747
- 42
- 398
- 622
-
Good reply. But this explains better (and also refers to the PEP 8 style guide, too): [The meaning of a single- and a double-underscore before an object name in Python](http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python) – paulsm4 Oct 07 '13 at 04:14
-
1@paulsm4, strangely enough I've left a number of upvotes on that question and its answers already. As SO grows it becomes difficult to remember all the good information it contains. – Mark Ransom Oct 07 '13 at 04:28