Python has no real private attribute. Every attribute is accessible. There are however two conventions indicating that an attribute is not meant to
be "public":
- attributes with a name starting with a single underscore (ex:
_myattribute
) should be seen as non-public and might change without
prior notice. They should not be used by third-party libraries or software. It is ok to use those methods inside the library defining them but it
should be done with caution.
- "class-private" attributes have a name starting with at least two underscores and ending with at most one underscore. These attributes' names
will be automatically mangled to avoid collision with subclasses' attributes. For example
__myattribute
will be renamed as
_classname__myattribute
, where classname
is the attribute’s class name without its leading underscore(s). They shouldn’t
be used outside of the class defining the attribute.
This rule raises an issue when a class-private attribute (two leading underscores, max one underscore at the end) is never read inside the class.
It optionally raises an issue on unread attributes prefixed with a single underscore. Both class attributes and instance attributes will raise an
issue.