From: www.itworld.com

Python: The one-size-fits-all programming language

by Nicholas Petreley

May 18, 2001 —

 

I'm a big fan of Python -- the open source programming language. In case you're wondering, Python is indeed named after the BBC comedy group Monty Python. The language was created around 1990 by Guido van Rossum, a fan of Monty Python. Since then it has matured at a phenomenal rate, thanks to its open source nature.

In its current state, Python miraculously manages to combine simplicity with incredible depth and breadth. On the one hand, Python is very similar to Java in terms of how one might apply it to build solutions and the kind of features it supports. Like Java, it is object-oriented, runs bytecode, and works well for server-side Web applications. Like Java, you can use Python to build client-side applets. There is a Netscape plug-in for this, but nobody seems to use it and it's not supported anymore. Why? Because you can write applets in Python and compile them into Java bytecode for use with any Java 1.1-enabled browser. These are but a few examples of Python's depth.

Unlike Java, Python is ideal for tossing together a quick script to do mundane work. If you're a casual Linux user, you have probably been executing many Python scripts and applets without knowing it. People use Python this way because Python is even easier to use in doing mundane tasks than Unix shell script languages, and it is infinitely more powerful. That demonstrates its breadth.

I don't mean to rekindle the Python versus Java wars that raged a few years back. Each language has its place. Neither is perfect for every need, although Python is closer than Java to being a one-size-fits-all language. But I happen to be as big a fan of Java as I am of Python. And I am drawing the comparison because you would do well to consider both if you're about to embark on a new Web application project. You just might find that Python is more appropriate than Java for your particular task.

One of the first things you'll notice about Python is its unique way of grouping statements. Python uses indentation to parse its code. Most C programmers tend to use indentation in order to make their code readable. You type an "if" statement, go to the next line, indent farther to the right than the "if" statement, and then type the code that is executed if the condition is true.

The difference is that in C you don't have to indent anything. You simply do so because that makes it easier to understand the programming logic when you come back to the code later. In contrast, you need to indent Python statements properly, or your program won't run. You can use tabs or spaces, or even mix the two, but you have to be consistent.

Indenting might feel rather odd when you first start programming in Python, however it takes just a few minutes to realize that it is not only an intuitive way to program, but it also pushes you to create code that is more readable, and therefore easier to maintain.

The other thing you'll notice is that it is as easy to create Python classes (objects) as it is to toss together a procedural script. So much so that you often end up using objects in Python even when you don't have to. That makes your code more reusable, which is always a good thing.

I've barely scratched the surface of what Python is and what it can do. If I've piqued your interest, visit www.python.org for more information. Once you have learned your way around Python and want to make your programming useful, I recommend that you pick up the O'Reilly book Programming Python by Mark Lutz, now in its second edition.

Next week I'll discuss some of the options available for server-side Web applications programming with Python.