Developer

Could Python put the squeeze on Java?

If you need a platform-independent application, you better use Java, right? Not necessarily, says Lamont Adams. In this article, he takes a look at Python and describes some of the features of this flexible language.


Let’s say you have to create an application that can run on a hodgepodge of different systems. You don’t have time to build something from the ground up, you need to integrate with a Web site, and you want clear, structured source so other developers on your team can provide some assistance when needed. Obviously, you should use Java, right? Well, maybe not. In this article, I’ll introduce you to Python, one of the best-kept secrets of the programming world.

What is Python?
According to the official Python Web site, Python is “an interpreted, interactive, object-oriented programming language.” That’s quite a mouthful; let’s dissect that statement:
  • Python is interpreted, much as Java and Perl are interpreted; neither of these can be compiled into machine code.
  • Python is also interactive. That is, code statements can be written and executed from inside an interpreter, reminiscent of BASIC and dBase, as opposed to noninteractive languages like Java, where the source code must be written in an external editor.
  • Python is object-oriented in much the same way that Visual Basic is object-oriented: Primitive types (integer, character, etc.) and some more complex types, such as files, cannot be treated as objects. Unlike VB, however, most everything else can be treated as an object, including function definitions. Python can also be used to create user-defined objects.

This official definition ignores one key feature of Python: its portability. By using Java’s technique of implementing operating system-specific functionality in C or C++ libraries and wrapping them in native language interfaces, Python applications enjoy quite a bit of portability. Although native to UNIX, Python interpreters are currently available for a variety of platforms including Windows, DOS, OS/2, Mac, Amiga, and even OS/390.

Something borrowed, something new
Python borrows a lot from C++, Modula-3, and Perl to arrive at a feature set that may seem eclectic at first pass. Errors are handled via an exception mechanism similar to that used in C++. Python programs can be organized into functions and modules, there is a clear syntax for doing so, and modules do not share variable namespaces.

One peculiar aspect of the language is that indentions are used to block code, thereby forcing the programmer to write cleanly formatted, easily read code. This sounds great on paper (or on computer monitor, as the case may be). But it can make deeply nested code actually harder to read and may prove frustrating, as the interpreter is very strict in enforcing those indentions.

Python variables are implicitly declared and are dynamically typed: They have no type until a value is assigned, and subsequent assignments can change that type. For example, a variable x can be assigned an integer value and can later legally be assigned a string value. Variable types are strictly enforced when expressions are evaluated or when passed to a function. For instance, if an attempt is made to sum variable x with a numeric variable, a runtime error will occur if x contains a string value, but not if x contains a numeric value.

One final thing to note here is that the name Python is taken from the 1970s comedy-variety television show Monty Python’s Flying Circus. References to skits from the show are actually encouraged in the language documentation. (“Say no more, nudge, nudge.”)

So what is Python good for, and who is using it?
Python’s creator, Guido van Rossum, when asked why he created Python said, “…I decided that I needed a language that was generally extensible.” He originally conceived Python as a shell-scripting and system administration language for the Amoeba distributed computing system. Python has grown beyond this decidedly narrow niche into a useful general-purpose language and seems to have accomplished its extensibility goal. Python appears almost infinitely extensible via its C API. Many extensions and plug-ins are available, giving Python programmers access to a variety of database engines, ActiveX controls, and even RS232 serial communications. This extensibility makes Python an ideal glue language, useful for tying together applications written in other languages.

Python is currently being used for:
  • CGI scripting by many Web sites, including Gamespot, PalmStation, Yahoo! and Nortel Networks Corporation.
  • General Linux system administration by RedHat Software. The RedHat Installer and RedHat Network Update Agent client are both written in Python, as well as the system configuration tools bindconf, apacheconf, and printconf.
  • An extension and plug-in language for NASA’s Integrated Planning System, the ObjectDomain Java CASE tool, and even the upcoming Star Trek Bridge Commander computer game.

A variety of variants and toolkits have cropped up for Python, including:
  • Tkinter, an interface for Tcl’s Tk GUI toolkit. This is the de facto standard Python GUI toolkit.
  • WxPython, an interface for the multiplatform wxWindows GUI class library.
  • ActiveState, which has a variety of products for Python, including Win32all, a Microsoft Foundation Classes interface, and Visual Python, a full-featured Visual Studio.NET IDE.
  • Jython, a 100 percent Java implementation of Python.
  • Pippy, a Python implementation for Palm development including the Python interactive interpreter.

So what is Python not good for?
Getting that pet shop owner to admit that the parrot he just sold is dead? But seriously, in light of the copious amounts of extensions created for it, there don’t appear to be any specific applications for which Python is ill-suited.

Instead, some features of the language itself may cause problems. While the language syntax is not difficult, the aforementioned fixation on indention will probably give some developers fits. The wide variety of available extension modules could potentially overwhelm a developer who is suddenly confronted by them. Since Python is interpreted, some programming errors that would normally be caught by a compiler, such as type mismatches, may not show up until runtime, causing debugging headaches. Python’s variable scope rules are a little counterintuitive and take some getting used to as well. Finally, the Python interpreter’s open-source status (it is produced under a GPL variant license) may carry a stigma with some organizations that are not already open source friendly.

 

Open source does not necessarily mean open source code
I should point out here that although the various Python interpreters are open source, this does not mean that an application developed with Python must be open source too. Python applications can certainly be distributed as source, but they can also be distributed in binary format.

 

There is also a minor security quibble that may concern some developers. Python’s byte code may be vulnerable to attack by a decompiler, similar to the problem experienced by Visual Basic developers prior to version 5. Although, to my knowledge, no one has yet accomplished this, it is certainly not beyond the realm of possibility.

Python vs.…
Python is usually compared with Perl, Tcl, and Java. Figure A shows how it compares with each one.

Perl In most cases, both languages are equivalent in terms of speed. Both feature powerful text manipulation functions. Both have regular expression evaluation features, although Perl's is arguably better than Python's. Python is clearly more modular than Perl. Perl supports pointers; Python does not. Perl.com has a more in-depth comparison here.
Java Although Java is generally faster, Python can outperform Java in some cases on the client side due to Java's VM overhead. Java is a more "pure" object-oriented language; Python currently does not allow user-defined objects to inherit from built-in objects. Both have large standard libraries. Neither language supports pointers. Python is supported on at least as many platforms as Java. Click here for a more in-depth comparison.
Tcl Python generally outperforms equivalent Tcl code. Python supports private namespaces, while all Tcl variables are global. Python allows selective handling of exceptions; Tcl does not. Python is better suited for complex projects. For more information, see this comparison.

Perl In most cases, both languages are equivalent in terms of speed. Both feature powerful text manipulation functions. Both have regular expression evaluation features, although Perl's is arguably better than Python's. Python is clearly more modular than Perl. Perl supports pointers; Python does not. Perl.com has a more in-depth comparison here.
Java Although Java is generally faster, Python can outperform Java in some cases on the client side due to Java's VM overhead. Java is a more "pure" object-oriented language; Python currently does not allow user-defined objects to inherit from built-in objects. Both have large standard libraries. Neither language supports pointers. Python is supported on at least as many platforms as Java. Click here for a more in-depth comparison.
Tcl Python generally outperforms equivalent Tcl code. Python supports private namespaces, while all Tcl variables are global. Python allows selective handling of exceptions; Tcl does not. Python is better suited for complex projects. For more information, see this comparison.

So are you ready to learn more? Good. You can get started by downloading the latest version of Python from the Python Web site, where you’ll also find a tutorial and online documentation. The distribution includes a command-line interpreter, an IDE, and a full set of documentation in HTML format. If you’d like to keep tabs on Python’s development, it’s hosted on SourceForge.net. Good luck and happy coding.

Editor's Picks