This convention is used for special variables or methods (so-called “magic method”) such as __init__
and __len__
. These methods provides special syntactic features or do special things.
For example, __file__
indicates the location of Python file, __eq__
is executed when a == b
expression is executed.
A user of course can make a custom special method, which is a very rare case, but often might modify some of the built-in special methods (e.g. you should initialize the class with __init__
that will be executed at first when an instance of a class is created).
class A:
def __init__(self, a): # use special method '__init__' for initializing
self.a = a
def __custom__(self): # custom special method. you might almost do not use it
pass