Home > Dive Into Python > An Object-Oriented Framework > Private functions | << >> |
diveintopython.org | |
Python for experienced programmers | |
Like most languages, Python has the concept of private functions, which can not be accessed outside their module, and private class methods, which can not be accessed outside their class. But the method of declaring private functions and methods is a little peculiar.
In this program, there are two functions: __getFileInfoObjectClass and listDirectory. They are defined the same way, but __getFileInfoObjectClass is private, because of the two underscores at the beginning of the name.
Example 3.4. Trying to access a private function
>>> import fileinfo >>> fileinfo.listDirectory <function listDirectory at 01112BAC> >>> fileinfo.__getFileInfoObjectClassTraceback (innermost last): File "<interactive input>", line 1, in ? AttributeError: __getFileInfoObjectClass
![]() | __getFileInfoObjectClass is defined, but because it is private, it can not be accessed from outside the fileinfo module. |
There is no explicit private declaration in Python. Anything whose name starts with two underscores and doesn't end in two underscores is private. This applies not only to functions in modules, but also to methods and instance variables in classes.
« A brief review | for loops » |