Python doesn’t have the concept of private methods or attributes. It’s all about how you implement your class. But you can use pseudo-private variables (name mangling); any variable preceded by __
(two underscores) becomes a pseudo-private variable.
From the documentation:
Since there is a valid use-case for class-private members (namely to
avoid name clashes of names with names defined by subclasses), there
is limited support for such a mechanism, called name mangling. Any
identifier of the form__spam
(at least two leading underscores, at
most one trailing underscore) is textually replaced with
_classname__spam
, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard
to the syntactic position of the identifier, as long as it occurs
within the definition of a class.
class A:
def __private(self):
pass
So __private
now actually becomes _A__private
.
Example of a static method:
>>> class A:
... @staticmethod # Not required in Python 3.x
... def __private():
... print 'hello'
...
>>> A._A__private()
hello