I Like Python

I recently started learning Python and to my surprise find it to be a very nice language.

I had heard good things about it years ago but never gave it a look. I noticed that Joel Spolsky gave Python half credit as a safe choice of language to develop enterprise applications in. Still, I was (and still am) happy working in Java.

What prompted me to look at Python is my new Job. I now work at BEA. The BEA WebLogic Server has a tool for configuring and managing domains called WebLogic Scripting Tool (WLST). WLST is based on Jython (Jython is Python implemented in Java).

I started reading a Python tutorial. The first thing I noticed is that code blocks are identified by indenting. This seemed very strange and uncomfortable after years of using curly brackets. Now it makes sense to me. I was going to indent my code anyway so why shouldn’t the language make use of that fact. A side benefit is no more arguing over if the curly brackets belong on the same line or a new line. If arguing about code style floats your boat be comforted that you can still argue over how many spaces to indent.

So far I have created two small utility programs in Python. One gathers some information about files and outputs an XML file. The other processes an XML file using SAX and outputs a new XML file. (I didn’t use XSLT because I needed to write files, run a program and include it’s output as new element content.) Even with the learning curve I was able to write these programs faster then I could have in Java. Also the code is much smaller than their Java counter parts would be.

Some things I like about Python:

  • Simple but powerful syntax is quick and easy to learn. It seems to me to be minimally complete. In other words it doesn’t have lots of unnecessary junk.
  • Very good documentation including tutorials.
  • The interactive interpreter makes it easy to try things out. This facilitates learning.
  • Like JavaScript, Python has a syntax for creating rich literal data structures. It is so much easier to do this:
    fruit = { "apples": [ "Cortland", "Braeburn"], 'pears': [ "Bartlett", "Bosc" ] }

    Than it is to do this:

    HashMap fruit = new HashMap();
    ArrayList apples = new ArrayList();
    apples.add("Cortland");
    apples.add("Braeburn");
    ArrayList pears = new ArrayList();
    pears.add("Bartlett");
    pears.add("Bosc");
    fruit.put("apples", apples);
    fruit.put("pears", pears);

  • List comprehension – a concise syntax to make new lists from existing lists without the usual for loop. Example: [ x**2 for x in range(5) ] produces this list: [0, 1, 4, 9, 16]
  • An extensive standard library of functions and classes.
  • The map() and reduce() functions.

Python will replace Perl as my scripting language of choice.