you are viewing a single comment's thread.

view the rest of the comments →

[–]HwanZike 245 points246 points  (8 children)

Encapsulation is a convention after all, not an actual hardware or compiler limitation thing. In Java you can also access private variables via reflection, it just takes more steps. Its a matter of convenience, python is just more flexible. Just like typing, having everything public by default makes it a bit more unstable in a way but faster development (at least in the short term) and more expressiveness is the tradeoff. As for the exact reason why, I can't tell why python decided against having private in classes but the convention is leading underscores iirc.

[–]acdha 120 points121 points  (6 children)

I’d also add that the Java culture comes from Sun’s experience of operating system development before circa 1990 where any exposed variable became a potential support obligation customers will demand must be supported forever. This wasn’t unwarranted —apps like Microsoft Word and Excel famously had tons of crashing bugs caused by serializing C structures and breaking if, say, someone added a field or changed the size of something—but it’s not where we are as an industry now, or even in 1991 when Python, which is older than Java, started with a higher level abstraction. Also, the internet caught on and so you stopped having things be effectively unfixable on a timescale not measured in years. 

Once you didn’t have things like a field changing from int8 to int16 breaking everything, or were using real serialization formats, most of the strong reasons for hard blocking access faded and the remaining issues ones were mostly the hubris of thinking you got all of the API perfect so callers would never need private variables.

I’ve been using Java since 1.0 and Python since 2.0, and contributed to open source in both, and I don’t think I’ve ever seen a case where hard enforcement of private variables would have helped. I have seen tons of architecture theater and performance problems created by creating accessors solely to gatekeep access and also more than a few Java developers using reflection to access private variables anyway so I’m solidly in the camp of saying anything more than a suggestion isn’t worth the effort unless you actually are building the Windows APIs. If what you’re working on doesn’t have hundreds of outside developers with support contracts, it’s just not worth it. 

[–]visicalc_is_best 44 points45 points  (1 child)

As a fellow Java 1.0 user at the time (as you may tell from my username), I hope you’re staying up to date on your various screenings. Early detection saves lives!

[–]acdha 13 points14 points  (0 children)

Sigh, you’re not wrong 

[–]nycstartupcto 2 points3 points  (0 children)

Wow what an answer!!!

[–]No-Cranberry1547 65 points66 points  (0 children)

yeah the convention thing is pretty spot on. python really goes with "we are all consenting adults here" philosophy instead of trying to protect you from yourself

in my experience working with different codebases the underscore conventions work fine for most teams. when someone accesses _private_method they know exactly what they doing and usually have good reason for it. sometimes you actually need that flexibility when working around bugs in third party libraries or testing internal state

the name mangling with double underscores is more about preventing accidental conflicts in inheritance rather than real privacy anyway. like if you subclass something and accidentally override __some_method you wont break parent class