#*** CS1/Python2Java.txt David MacQuigg 26-Oct-07 Comparing Java to Python. Notes as a Python programmer learning Java. #** Privacy in Python Privacy in Python is not "enforced". The Python programmer can label various parts of a program private, but the user can ignore these warnings and use the private parts anyway. Privacy in Java is enforced, and that may be an advantage in large, proprietary projects. In Python, there are two ways to make a name private. By default, everything is public, but a leading underscore on a name will make it private. You can also add a special name __all__ to a module, and explicitly list the public parts. Everything else in the module will then be private. For example, in module Coins.py, we have: __all__ = ['BiasedCoin', 'FairCoin'] # all items in the public API # everything else is "private" class _privateCoin: '''A leading _underscore makes this class private.''' class TemplateCoin: '''This class is not included in the __all__ list, so it too is private.''' Private just means it won't be imported in a normal 'import' statement. >>> dir() ['__builtins__'] >>> from Coins import * >>> dir() ['BiasedCoin', 'FairCoin', '__builtins__'] As you can see, only the public parts were imported. We can still see the private parts if we want to, however: >>> import Coins >>> dir(Coins) ['BiasedCoin', 'FairCoin', 'TemplateCoin', '_PrivateCoin', '__all__', '__builtins__', '__doc__', '__file__', '__name__', 'random'] We can even instantiate a private class and run its private methods. >>> pCoin = Coins._PrivateCoin() >>> dir(pCoin) ['__doc__', '__module__', '_privateMethod'] >>> pCoin._privateMethod() Didn't you see all those _underscores? Keep out of my private parts, you peeper! A Python programmer might say "Python is a language for consenting adults.", or maybe "We don't need a nanny!". Full access to private parts is also a great help when debugging. #** Hiding vs Encapsulation Many texts put a lot of emphasis on "hiding" when it is really the same as encapsulation. Examples of "hiding" offer no real secrecy or security as the plain-English meaning of the word implies. "Hidden" items can be discovered by simply looking at the source code. Keeping source code secret, and distributing only compiled programs, does offer a weak form of security (often called "security by obscurity" by cryptographers and others who need true security). But that is not the reason for the emphasis on "hiding" in programming texts. The real reason is the "heuristic power" of the concept "hiding". To quote Steve McConnell[1]: "Information hiding has unique heuristic power, a unique ability to inspire effective design solutions." If you think about hiding something, you will do a better job of encapsulating it, even if you can't really hide it. In a sense, the programming community has redefined the meaning of the word "hiding". Rather than let that be a barrier to our discussion, or a replay of an old debate [2], we will use the term "hiding" as most programmers use it, and not in the sense of establishing any true secrecy or security. [1] Steve McConnell, "Hide Secrets", p.92 in "Code Complete", S-5.3 "Design Building Blocks: Heuristics". [2] See also Parnas 1972, Brooks 1995, for historic debate on the topic. #** Different operators Python Java x**y none Exponentiation and or not && || ! Logical none >>> Unsigned right shift #** See also: http://www.ferg.org/projects/python_java_side-by-side.html - "Java & Python, A Side-by-Side Comparison", Stephen Ferg, upd 2007-05-01 http://www.artima.com/intv/strongweak.html - "Strong vs Weak Typing, A Conversation with Guido van Rossum", Part V by Bill Venners with Frank Sommers, February 10, 2003. http://mindview.net/WebLog/log-0025 - "Strong Typing vs. Strong Testing", Bruce Eckel, 05-02-03 http://dirtsimple.org/2004/12/python-is-not-java.html - "Python is not Java", Phillip J. Eby