Single underscore
It is used before an object’s name to indicate other programmers that the method is meant to be private. It is also used in the name of methods which are fully intended to be overridden by subclasses. Ex- _abc
Double underscore
They are used for fully private methods. Ex- if your class is intended to be subclassed and you have attributes which you do not want the subclass to use, you can name them using a double underscore.
The rules of underscores in Python remain the same whether it is an object or a function, etc. Let us see an example of class for better understanding of the difference between the single and double underscores.
Example-
>>> class ThisClass():
def __init__(self):
self.__superprivate = "Orange"
self._semiprivate = ", Apple"
>>> my_class = ThisClass()
>>> print my_class.__superprivate
Traceback (most recent call last):
File "", line 1, in
AttributeError: thisClass instance has no attribute '__superprivate'
>>> print my_class._semiprivate
, Apple
>>> print my_class.__dict__
{'_ThisClass__superprivate': 'Orange', '_semiprivate': ', Apple'}