Single or double underscores at the beginning and/or at the end of names have special meanings in Python:
_single_leading_underscore
: Weak “internal use” indicator. For example, from M import * does not import objects whose name starts with an underscore.
single_trailing_underscore_
: Used by convention to avoid conflicts with Python keyword, e.g. class_ instead of class.
__double_leading_underscore
: When naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo
)
__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.