_nombre(prefix with a single underscore): Indicates that the name is for internal use. When importing a module with from m import *, no names starting with _will be imported.
It is also used in classes as a way to indicate that a certain method is for internal use and is not part of the public API, but this is convention and is not given any special meaning by the language.
__nombre(double underscore prefix, maximum one suffix underscore): Indicates that a certain member of a class is private, and converts the name via name mangling to _clase__nombre, where class is the name of the current class.
The intent of name conversion is to avoid accidents, for example when inheriting from a class with "private" attributes or methods. It does not prevent in any case its overwriting or access in general.
__nombre__(prefix and suffix with double underscore): Indicates that it is a magic name . In general, these are either special variables or methods with a special meaning or behavior within the language. Never define your own magic names, just use them as documented .
In addition, the following special forms with initial or final underscores are recognized (these can generally be combined with any convention):
_single_leading_underscore : weak "internal use" indicator. For example: from M import *don't import objects whose name starts with an underscore.
single_trailing_underscore_ : used by convention to avoid keyword conflicts Python, e.g.
Tkinter.Toplevel(master, class_='ClassName')
__double_leading_underscore : When naming a class attribute, invokes name renaming (inside the FooBar class, casts __booto _FooBar__boo;).
__double_leading_and_trailing_underscore__ : "magic" objects or attributes that live in user controlled namespaces. For example: __init__, __import__or __file__. Never invent such names; only use them as documented.
_nombre
(prefix with a single underscore): Indicates that the name is for internal use. When importing a module withfrom m import *
, no names starting with_
will be imported.It is also used in classes as a way to indicate that a certain method is for internal use and is not part of the public API, but this is convention and is not given any special meaning by the language.
__nombre
(double underscore prefix, maximum one suffix underscore): Indicates that a certain member of a class is private, and converts the name via name mangling to_clase__nombre
, where class is the name of the current class.The intent of name conversion is to avoid accidents, for example when inheriting from a class with "private" attributes or methods. It does not prevent in any case its overwriting or access in general.
__nombre__
(prefix and suffix with double underscore): Indicates that it is a magic name . In general, these are either special variables or methods with a special meaning or behavior within the language. Never define your own magic names, just use them as documented .You can find these conventions in the PEP 8 .
This is found in the documentation,
Python: Naming Styles (English)
Where what Darkhogg comments is described