Monday, March 20, 2006

Python, not reptilian (anymore)

Python, a hacker's tool and nothing more till a couple of years ago has suddenly burst into the limelight. I know hardcore Python fans are gonna flame me for calling Python this, but the language has matured and is usable by normal programmers only in the last couple of years. My definition of a normal programmer is someone who programs (writes code, designs stuff, graffiti and the works) day-to-day but is not addicted to it so much so that sleeping and bathing become a tiresome ritual. Let me leave out the hacker demi-gods who are happy with even LISP from the discussion on language usability for time being. They could take LISP and Perl, bang their heads and make it work, so I guess language usability/elegance might not matter much to them.

So what is so good about Python that makes it usable to the programmer community in general? I guess I would seek to answer that during the course of this post.


1. Python promotes adherence to coding standards, indentation for example. Indentation, IMHO should not be treated as an add-on feature (that is optional) but is something absolutely necessary to produce readable code. Python enforces this by making sure that blocks of code should follow the same level of indentation. For e.g. a "for" loop will work only if all the statements in it have the same level of indentation, though you are allowed to throw in braces for that good 'ol feeling. Braces don't make a difference here to the Python interpreter, the indentation surely does. (Braces signify a dictionary in python, a datatype akin to a hash table in other programming languages)

2. Python allows/encourages easy plugging in of modules. This might not be something unique to Python, but it's still wonderfully implemented. Python modules to get the most common programming tasks done are readily available and are a few key strokes away from your pet project. Python might not boast of something equivalent to CPAN (for PERL), but it’s just that re-usable Python code is all around the place and not centralized like CPAN.

3. Python is cross platform across OSes (so is PERL and C++ and Java). I believe its the best cross platform implementation (albeit with heavy favoritism for Unix like systems). It doesn't need a third party Virtual Machine (VM) to run it (it however needs python to run), nor does it rely heavily on system libraries to make it powerful. PERL is good, but I haven't seen too many PERL programmers on Windows, not yet.

4. Python is interpreted. This might be construed as a disadvantage by many because compilers are supposed to be faster and can dole out better assembly. But Python breaks that notion and is very fast for an interpreted language. Don't believe me? Try it out, it’s only a bit slower than comparable C code and C is the very definition of speed amongst high level languages. -- Python source code is automatically compiled into Python byte code by the CPython interpreter. Compiled code is usually stored in PYC (or PYO) files, and is regenerated when the source is updated, or when otherwise necessary. This bytecode is executed by the interpreter at runtime. There are options to compile a binary instead of bytecode using tools like py2exe.

5. Python is actually compiled into an intermediate form called bytecode which can then be run in a traditional interpreter. The advantage of this bytecode is its platform independence. Bytecode generated on Windows would run without any (major) modifications on UNIX flavors. However, it is to be noted that bytecode is not portable across versions of Python, so bytecode generated by Python ver 2.2 might not be compatible with Python ver 2.5.

6. Python is excellent when you want to try out some small piece of code (for say a proof of concept or an experiment). You don't need to worry about traditional boiler plate stuff here, most of that is taken care already. So you can start hacking immediately and almost all the code that you write would be actual, executable code and very little fluff.

7. To extend a little bit on point 6 (because it’s very important), Python is the best language for prototyping. When you want to hand out a prototype/model to a client for review, Python allows you to do just that, no frills attached. Its famous Rapid Application Development (RAD) paradigm is not restricted to words and you can see the difference when you start coding. I cannot emphasize enough on this point, because it could save man hours of work. A prototype which I develop over a week (without much effort) is easily throwable than something developed by a dozen guys in a month. So I would not stick with code which is not usable just because I took time to write it. What if the client changes the entire requirement/design after the prototype is ready (they are famous for it, aren't they). If I have a cute little Python prototype which does everything that the 'real' thing needs to do and it was developed in a short span of time, I can use it or throw it and not much is lost (except a few broken Python fans' hearts maybe).

8. Python is well integrated with some of the most famous libraries/toolkits out there. Python can integrate with COM, .NET, and CORBA objects. For Java libraries, use Jython, an implementation of Python for the Java Virtual Machine. For .NET, try IronPython, Microsoft's new implementation of Python for .NET. X, Tk, Motif and a gamut of other graphical toolkits are also readily available for use within your Python project, so GUI development is a snap, like never before.

9. Python is extensible. If any portion of your Python module needs speeding up, you have the option of writing it in C/C++ and integrating it with the rest of the stuff using Swig or Boost Python (there might be more, I could think of only Swig and Boost now). A brief note here, Python is good enough or quick enough for most requirements and embedding C code might really not be necessary, but you have the option nevertheless.

10. Python looks good. Isn't that silly, you would say. Why should a programming language 'look' good. I guess code needs to look good and promote readability/reviewability even if its months since you wrote it and not obscure to the point of losing some hair to understand what you wrote a couple of months ago. Most programmers spend more than half of their time reading code written by others. So its of utmost importance that code written by everyone is readable. PERL is notoriously famed for its obscure syntax which allows nearly un-decipherable code (at least for humans) to be written. If your code looks ugly in Python, it most probably is not going to work either. How do you find out if your code is ugly? I guess its more of gut instinct to start with and becomes an art with experience. So is it impossible to write code in Python that looks ugly, lacks clarity and yet works? Well, I guess someone can actually try it out as an exercise but be rest assured that Python will make it at least a dozen times tougher than PERL to write obscure code.

11. Python promotes concise code and takes obscure code out of the picture by providing easy and beautiful ways to write working code for pretty much everything. You don't need to write obscure code just because your application is really complex, you would still have an elegant way out in Python and that elegant way would be easy and obvious for most programmers. C is elegant alright, but the most elegant solutions in C might not be the most obvious.

What else in Python deserves mention, well its a long list. Visit some links I have given below and they should help you get a better idea and even a head-start with the language, if you are not already initiated.

Quotes about Python: http://www.python.org/about/quotes/

Python success stories: http://www.python.org/about/success/

Getting started: http://www.python.org/about/gettingstarted/

Do a Google on Python and you should get a wealth of information to aid you in your quest.

Welcome to the world of Python and Happy Hacking!