(Replying to PARENT post)

Forgive my incompetence, but I have never gotten the point of private methods/variables. Could someone please explain why they exist?

Edit: I know it's slightly off-topic.

๐Ÿ‘คlukeqsee๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0

(Replying to PARENT post)

One of the points not discussed in the other replies yet, is that public methods and variables are the interface, the well defined, relatively static way of using a class. The private members are internal implementation details, and may change frequently -- not to mention completely change in style, functionality and so on.

The idea then, is that larger teams can be divided into sub-teams, and work in parallel because the interfaces between components don't change, even if the internals change drastically. Essentially it allows components to be somewhat independent. How this works in practice tho...

๐Ÿ‘คsophacles๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0

(Replying to PARENT post)

Arguably, it's a matter of isolation from 'irrelevant' changes. In a function, one wishes to keep the ability to redefine their local variables (perhaps, to implement a faster/smaller-space algorithm) without otherwise affecting the overall program -- when they call other code, that other code should avoid accessing the caller's variables, lest the caller be stuck with precisely those variables and that implementation. If this isolation exists, I have more freedom to vary the implementation.

Likewise, private members of a class are implementation details I may wish to change later -- but I would like some assurance that other components will be isolated from this change, so as not to break.

Edit-add: oh, I of course forgot to mention -- for those not familiar, python permits both :-)

๐Ÿ‘คToastOpt๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0

(Replying to PARENT post)

I may not be the most qualified person to answer this, but I always thought private methods and variables were for internal use inside of a class while the public methods were what one would actually call with their objects. For example your object had a method foo() which would be better defined as smaller methods which are combined together. Those smaller functions serve no other purpose and therefore do not need to be called by the object directly.
๐Ÿ‘คsam191๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0

(Replying to PARENT post)

Private members are generally just to protect your from yourself. If you have an integer that should /always/ be 1-10, it's easier to use a private variable and a setter that ensures the variable is always valid, than to hope all your code follows the rules. (Then also if the rule changes, e.g. to 5-100, you just need to change one function).

There are some other uses and examples, but this is the simplest one i can think of off the top of my head.

๐Ÿ‘คianp๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0

(Replying to PARENT post)

Access specifiers (like "private") are language features used to implement the OOP notion of encapsulation: http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_.... Encapsulation is generally cited as one of the defining features of object oriented programming.
๐Ÿ‘คrryyan๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0

(Replying to PARENT post)

Private methods/variables etc are those which are not publicly available. Typically internal state for the object.

Ideally, or at least for purity, all variables should be private and only accessible via get/set methods, thus encapsulation is enforced.

๐Ÿ‘คepo๐Ÿ•‘15y๐Ÿ”ผ0๐Ÿ—จ๏ธ0