4 ms·
This may sound super n00b but I really like that Java is explicit about everything. So for instance, you explicitly specify variables, methods as private with t
by pm90 8y ago
This may sound super n00b but I really like that Java is explicit about everything. So for instance, you explicitly specify variables, methods as private with the keyword 'private'. In python you have to use an underscore, and its still not really private, its obfuscated. That's just one example, but in general I really like that kind of by-the-book verbosity, instead of having to mentally map it into the OOP concepts.
- xapata 8y agoWhy would you want to actually enforce privacy? That makes it harder for the user to patch. Better to provide a mild discouragement via "_name".
- twblalock 8y agoBecause 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.
- xapata 8y agoYou 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.
- marvin 8y agoThat'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.
- StavrosK 8y agoI 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".
- ychen306 8y agoThe 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).
- chomp 8y agoPrivate doesn't mean much. You can still access private members in Java using reflection.
- kjeetgill 8y agoThis hole is incrementally being closed with the introduction of Java 9's modle system. You can only deep reflect into classes in your modules or those "open" to you. There are both declaritive and command line param ways to open things so it's not as rigid as it sounds.
- ben-schaaf 8y agoDouble underscore is the real 'private': >>> class Foo: def __fn(self): return 2 >>> Foo().__fn() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Foo' object has no attribute '__fn' >>> Foo()._Foo__fn() 2 A single underscore is more like 'protected', ie. it doesn't prevent subclasses from easily/accidentally overriding your function.
- kimdotcom 8y agoCheck out Ada.
- kjeetgill 8y agoI'm less concerned with encapsulation, but the explicitness is a godsend the larger your codebase or team. Python keeps code much smaller but above a certain size I wish I could "promote" parts of my code to Java. Partial typing in Python just isn't close to feeling the same.