News for hackers. Stuff that matters.
Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:24PM (#17570864)
Why would you want to actually enforce privacy? That makes it harder for the user to patch. Better to provide a mild discouragement via "_name".
Re:Python has brought computer programming to a vast new aud... by twblalock on Thursday July 19, 2018 @06:35PM (#17570945)
Because in traditional object-oriented design you would extend rather than patch. It's the open-closed principle.

There are many benefits to enforcing privacy, and one of the biggest ones is that you can make changes to the internals of your library without breaking your users' code, because they never had access to the internals.

Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:42PM (#17570985)
You can get that benefit with Python. Name mangling helps. It gets the benefits of `final` without the annoying enforcement.

Don't stop me from patching your buggy code. If writing an extension is more efficient, that's how I'd solve the problem.

Re:Python has brought computer programming to a vast new aud... by marvin on Thursday July 19, 2018 @07:03PM (#17571118)
That's an eternal ideological debate where there is always a rebuttal on the form "a-ha - but you could just...", but the rationale is this: Your class will probably be doing things with the private variables that aren't obvious, and will break things if you access them from outside. (Either by them having a different value than you expect, or expecting a different value than you set).

Of course, if you have a perfect understanding of the code, this will never be a problem, as the program is always executed as it is written. But most likely, not everyone in your organization will be as patient, smart and attentive to the details as you are.

Re:Python has brought computer programming to a vast new aud... by StavrosK on Thursday July 19, 2018 @07:51PM (#17571373)
I don't understand the argument, though. Python's underscore variables are meant to not be accessed from outside. The argument against them isn't that you might be doing some non-obvious things inside the class, that's the whole point of them!

It's a way for the class user to say "I know this is terrible, I know this might break, I want to do it anyway".

Re:Python has brought computer programming to a vast new aud... by ychen306 on Thursday July 19, 2018 @09:36PM (#17571802)
The obvious usage of privacy is so you don't expose an API you don't want to maintain. It's also occasionally used to maintain some invariants; for instance, you can have a public method that wraps around some protected interface that does input checking/clean up. Having the compiler enforcing privacy, in this case, guarantees that whatever actual method you dynamically dispatches to has the "right" input (accidentally calling the interface directly won't compile).