I understand how it works and I understand meaning of syntax. But I do not understand why would I want to use it?
-
5I don't think this is an appropriate question for stack overflow. There is a massive amount of information on object oriented design and the benefits of private vs. public available online. – Pete Jan 04 '13 at 14:02
-
1Do you hand over your wallet and ID's any time someone asks for your name? If someone asks "Do you have a dollar I can borrow?" Do you give them your account and PIN so they can look? – asawyer Jan 04 '13 at 14:02
-
6You don't understand the syntax for private methods in Python, because there isn't any. – Wooble Jan 04 '13 at 14:02
2 Answers
There are no private members, including methods, in Python. The double underscore prefix is actually just a “convention” for private members which is additionally inforced by Python as the names are further mangled. It’s not impossible to access these methods from the outside though.
In general though, there is no real need to make members really “private”. Often, a single underscore prefix is used to mark members as “internal”, and users of the types are asked to keep them alone.
Other than that, of course the reason to still have private (or internal) methods would be to extract common parts of functionality into a method so you don’t have to repeat yourself.
For more information on the missing private-ness of Python members, see this question.
-
And, of course, to tell the user of your module "don't call this method yourself and if you do, don't blame me when it disappears in the next version with no warning" – Wooble Jan 04 '13 at 14:09
sLet's say you have operation operationA that calls to subOperationA and suboperationB on the same class, and those methods have no meaning as individual operations, they manipulate data and you have to prevent unexpected executions of them (that means calls from methods others than operationA). So private allows you to protect and encapsulate your methods, limiting their visibility only to the desired scope.

- 779
- 5
- 18