Python is Not Java

Phillip Eby wrote an article on his blog titled Python is Not Java, stating the common mistakes Java programmers often made when they tried to write a Python application, due to different idioms used in each language. While I agree that you cannot simply convert something line by line from one language to another, Eby's points are more like rants than constructive statements.

  • Static methods in Java should be translated into module level functions in Python.

    No problem with that, and I agree that in the Python land that flat is usually better than nested, due to expenses of dictionary lookup, especially in tight loops.

  • switch() {...} in Java should be translated to hash tables.

    Indeed that if ... elif ... is not an efficient solution to translate a switch() {...} statement. However, I still failed to find an equivalent in Python, because:

    1. Hash table (or a jump table with Python objects as the keys) requires much higher set up cost, at coding time as well at run time.
    2. Hash table algorithm is not as efficient as switch, if the comparison is over primitive types.
  • XML is evil and no Pythonists should use it except the most stubborn ones.

    Again, I do not see why XML-bashing is necessary. There are plenty of good uses of XML, even when your main application is coded in Python. After all,

    1. XML is a representation of a document, and is not a replacement for your code. Even (a good) Java programmers do not write XML documents to replace their Java code.
    2. Python might still needs XML when it tries to communicate with other applications, who might happen to use XML. Working with files? Acting as SOAP/XML-RPC client/server?
    3. Python's XML parsing can be extremely slow, if you are using a full validating parser in PyXML. That could be one reason why Python developers hate XML.
  • Python classes should not use getters and setters, which are evil.

    Getters and setters are not evil, and even Python's property() hack is based on it. Saying that it wastes CPU time is ignorant, as Python's property() can be as expensive as getXXX() and setXXX(). Moreover, property() was not available until Python 2.2+, so you'll still find getters, setters or modified __getattr__() in old Python code.

    I careless about which one is used anyway, as long they stay consistent.

  • Java forces you to write duplicate codes.

    Maybe we should not get onto discussions of static verses dynamic types. Both have their pros and cons. You do not have to write duplicate code in Java, if you can risk wrapping everything in java.lang.Object, and then cast them back into the type you want. Python's dynamic nature can sometimes be dangerous at run time, if not enough checking has been put into place.

Learning a computer language is indeed more than just learning the syntax and its supporting libraries, but also its way of doing things. However, I just do not see a rant like this is going to help Java programmers to settle in the Python land.