This question already has an answer here:
Ok I think I have understood the use of one and two heading underscores in Python.
Correct me if I am wrong,
In the case of one underscore, the underscore prevents the
from X import *
statement to import this kind of variables.In the case of two underscores, the variable's name is prepended with the name of the class it belongs to allow a higher level of "privateness".
My question now is: why not use two underscores only? In which cases one underscore is preferred (or needed) over two underscores?
Short answer: use a single leading underscore unless you have a really compelling reason to do otherwise (and even then think twice).
Long answer:
One underscore means "this is an implementation detail" (attribute, method, function, whatever), and is the Python equivalent of "protected" in Java. This is what you should use for names that are not part of your class / module / package public API. It's a naming convention only (well mostly - star imports will ignore them, but you're not doing star imports anywhere else than in your Python shell are you ?) so it won't prevent anyone to access this name, but then they're on their own if anything breaks (see this as a "warranty void if unsealed" kind of mention).
Two underscores triggers a name mangling mechanism. There are very few valid reason to use this - actually there's only one I can think of (and which is documented): protecting a name from being accidentally overridden in the context of a complex framework's internals. As an example there might be about half a dozen or less instances of this naming scheme in the whole django codebase (mostly in the django.utils.functional package).
As far as I'm concerned I must have use this feature perhaps thrice in 15+ years, and even then I'm still not sure I really needed it.
Look at documentation.
1. Single Underscore
From PEP-8:
2. Double Underscore:
From the Python tutorial: