Wednesday, June 22, 2011

Python Vs Java

After using Python for the past several years, I'm going to be taking on a Java project. I am in the process of learning Java, and I thought I would write up a comparison of the features in each language.

Features Java has that Python doesn't:
  • Static typing
  • Strict access control (package, public, protected, private)
  • Traditional threading implementation wrapped in a decent API
  • Bytecode backwards compatibility 
Features Python has that Java doesn't:
  • Dynamic objects
  • No explicit compile step
  • Properties (transparent getters/setters)
  • List comprehensions
  • Operator overloading
  • Generators (create iterators with the 'yield' statement)
  • Optional keyword arguments, *args, and **kwargs
  • pypi (the cheeseshop) and pip
Static Typing

Static typing has 2 main advantages. Type errors can be caught at compile time, and the compiler can make more optimizations. Statically typed code takes more time to write (creating explicit interface definitions), but you don't need to write manual type checking functions (duck typing) like you often need to in a dynamic language. Static typing also allows you to avoid runtime type errors that you would probably need a unit test to catch with a dynamic language. Javascript and Python both have decent JIT compilers available, so the speed difference between dynamic languages and static languages will continue to narrow.

Strong vs Weak Typing

While static typing may be helpful for some projects, I believe the biggest factor in type usability is not static vs dynamic but strong vs weak. Weakly typed languages allow you to cast objects from one type to another. If you've ever used Perl, PHP, of Javascript you've probably run into some hard-to-debug problems that were caused by implicit casting or automatic type coercion. Languages like these usually have confusing operators like '==='. C doesn't do any implicit casting, but it will let you manually cast in unsafe ways. Java also allows casting, but is safer than C, as it will throw a runtime exception if you try to cast to an incompatible type. On the other hand, Python is strongly typed. There is no such thing as a 'cast' in Python, and there are very few situations where automatic type conversion takes place (arithmetic with operands of different number types automatically convert all operands to the widest type used in the expression). Python's combination of strongly typed objects and dynamic objects with duck typed interfaces is a winner.

Access Controls

Python has no access control modifiers, and instead uses a convention of naming private attributes with a leading under score: '_private_method'. Client code is not 'supposed' to use attributes named with a leading underscore, but there is nothing technical stopping it. I must admit there have been several times when I wish Python had some equivalent construct. Java's 'final' modifier is particularly useful. Considering that many of Python's standard data types are immutable (string, unicode, tuple, frozenset), it's surprising that Python does not offer an easy way to define immutable objects. In Java it's as easy as adding 'private final'.

Threading

Unlike Python and the GIL, Java can utilize multiple cores when executing threads, and it's concurrency interface is wrapped in a nice API. However, I'm not sure how much I'll get to use the threading features. Networking code is increasingly being moved away from threaded implementations to asynchronous/non-blocking solutions such as NodeJS and Twisted, and computation is being distributed on a cluster or 'in the cloud', instead of being run on a single machine.

Other Features

Java lacks many useful features present in Python, and I'm sure I will miss many of them. I hope the list of useful Java constructs grows as I learn more about the language and start working on a production code base.



5 comments:

  1. check out Groovy, get back more dynamism for Java devs

    and then Grails for Django?

    ReplyDelete
  2. HI, great article; i love PyJa war :D
    I follow you from ACL post! :)

    Just onr point:
    In python you can declare a "private" object with a double _ [not only one] and, yes, you can continue to "control" it but in a different way (Why i've to cut my ball? :D). But for me is an interesting test/debug feature and not a lack :)

    for example a variable called self.__spam in a 'A' class can accessed with self._A__spam, but if you try with self.__spam you'll raise an AttributeError


    >>> class A(object):
    ... def __init__(self):
    ... self.__spam = 'ham'
    ... def __egg(self):
    ... print 'private'
    ...
    >>> a = A()
    >>> a.__spam
    Traceback (most recent call last):
    File "", line 1, in
    AttributeError: 'A' object has no attribute '__spam'
    >>> a.__egg()
    Traceback (most recent call last):
    File "", line 1, in
    AttributeError: 'A' object has no attribute '__egg'
    >>> dir(a)
    ['_A__egg', '_A__spam', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
    >>> a._A__spam
    'ham'
    >>> a._A_egg()
    Traceback (most recent call last):
    File "", line 1, in
    AttributeError: 'A' object has no attribute '_A_egg'
    >>> a._A__egg()
    private

    see u,
    Andrea, z4r

    ReplyDelete
  3. __methods are somewhat private, but the Python community has labeled them 'unpythonic'. __methods are considered to be a deprecated feature by many Python coders, and they are used very rarely in modern Python code.

    ReplyDelete
    Replies
    1. I hadn’t realized that `__methods` were deemed unpythonic. Good to know! Thanks. :-)

      Delete
  4. So I saw your profile on StackOverflow and I'm also a LIMS coder. I find myself using Jython almost every day for miscellaneous tasks related to LIMS development.

    ReplyDelete