You Used Python to Write WHAT?

Python is a powerful, easy-to-use scripting language suitable for use in the enterprise, although it is not right for absolutely every use. Python expert Martin Aspeli identifies when Python is the right choice, and when another language might be a better option.

By Martin Aspeli

CIO|Feb 19, 2008 7:00 AM
PT

Programming language decisions often come down to personal preference and experience. Most modern languages are capable of performing the majority of programming tasks and include the necessary libraries to be useful day to day. Sometimes, interoperability concerns can dictate a particular platform, but nowadays, interoperability is commonly best achieved through XML interchange, shared SQL databases or Web services.

Therefore, when choosing a language for a particular purpose, it is often more important to look at how a language is designed, what it makes particularly easy, and what it makes more difficult. If features or performance do not detract, intangibles such as "feel," "elegance" and a sense of programmer productivity should be given serious weight.

Python is a powerful, opinionated and idiosyncratic scripting language, loved (and hated) by programmers the world over for its style, syntax and attention to whitespace. It excels as a "glue" language for putting together applications quickly, and many Python developers feel more productive in Python than in other languages. This article shows you why, and also points out situations when Python is perhaps not such a good choice.

First, let's take a quick look at the way Python works: a very short technical overview (suitable even for nontechnical managers).

To give you a feel for how Python looks, here is a short code snippet:

You may not know much about Python, but you can probably guess what's going on. This is Python's single best feature: Things generally work the way you expect. This obviousness in syntax makes the language relatively easy to learn for new programmers and easy to remember for occasional ones. However, the fact that it differs substantially from most other languages can be a barrier.

Programming the Way Guido Indented It

Python was created by Guido van Rossum, its "Benevolent Dictator for Life." The language and its standard library are developed by a thriving open-source community, but under Guido's watchful eye, Python's consistency and spirit remain intact. First released in the 1990s, Python is still evolving today.

Python is fully object oriented and includes a few functional programming constructs. It also has built-in support for commonly used data structures such as lists, dictionaries and sets. Its creators emphasize readability, consistency and simplicity; they believe that programming languages should be concise, but not too clever for their own good.

The main implementation of Python is written in C and runs on virtually any modern platform. There are also implementations that run inside a Java Virtual Machine (Jython, JPype), on the .Net platform (IronPython) and even one written in Python itself, called PyPy.

The C implementation is highly optimized, and is usually more than fast enough for normal programming tasks. However, if raw speed is your primary priority, look to a compiled language such as C. For embedded systems with limited memory, Python's runtime overhead may also be a problem.

Python as a General-Purpose Language

Python is the default choice of scripting language for many developers. In the words of one Pythonista, it is rare to start a project with Python and discover that it was an entirely inappropriate choice as it grows, because Python scales both in project size and performance. That said, the degree of freedom that the language grants developers means they sometimes have to be a little more disciplined in how they structure their code.

It takes almost no effort to get started with Python. At its simplest, you can just launch the python interpreter and type away in interactive mode. The results of your statements are printed to the console immediately:

Of course, this is useful only for very simple tasks, but save those statements to a file with a .py file, run that file through the interpreter and the script is executed.

As programs grow more complex, developers may define functions and classes and split code across multiple modules, or source files that make up the same program. Modules can be organized into packages, which can be turned into distributable, self-contained bundles (known as eggs).

You can find thousands of free Python packages at the Python Package Index. For day-to-day tasks, Python's standard library includes everything from shell interaction to file management, XML and CSV manipulation, and much more.

Python has a strong role in business computing, particularly in Web and enterprise development. Let's take a look at when it's the best (and not-so-best) choice.

Python on the Desktop

You can write desktop applications in Python using frameworks such as WxPython or PyGTK. However, most desktop applications are still written in compiled languages such as C, C++ or C#. The frameworks for these languages tend to have more sophisticated development tools and the resulting programs are often easier to distribute, as they do not require the user to have Python installed.

Python has good graphical development tools, including Wing IDE and the Eclipse PyDev extensions. However, most Python developers work "Unix style" with standalone text editors and terminals. On platforms like Java or .Net, environments such as Microsoft's Visual Studio will always offer tighter integration with the programming language. Whether this is a blessing or a curse depends on what type of developer you speak to.

Python for the Web

Much has been made recently of an alleged exodus of Java programmers, who, fed up with Java's overhead and its enterprise frameworks, are making the leap to Ruby on Rails and "lightweight" rapid Web development. The Rails proponents are very good at marketing, but most of the same benefits can be found in the Python world.

In fact, several successful rapid Web application frameworks are available for Python, each with its own slant. Many also share components. The most popular include Django, Pylons, TurboGears, CherryPy, Zope and Grok (which is based on Zope).

These frameworks are all suitable for serious applications. Zope, for example, was a pioneering open-source application server that helped prove Python's viability in the enterprise (although many Python developers these days feel it is a little "unPythonic"). Plone, a popular open-source content management system—to which the author is a contributor—runs on Zope and has been implemented in organizations such as Novell and Oxfam. The high-traffic Reddit.com runs Pylons. The Revver.com video sharing site uses Django.

Deploying a Python Web application is usually straightforward, although not quite as easy as deploying a PHP application in Apache. Database connectivity is very well catered to by object/relational mappers such as SQLAlchemy. However, most Python Web frameworks have yet to catch up to enterprise-grade application servers for Java or .Net in terms of support for high-availability clustering, failover and server management.

Python in the Enterprise

Many large organizations have standardized their development on one of the two main "enterprise" platforms, Java or .Net, believing that doing so will improve interoperability and lower maintenance costs. Although Python does not quite operate with the same ubiquity or scale, it is a very useful complement, and Python is a solid alternative when such platforms are inappropriate.

The traditional enterprise platforms are by necessity large and complex. They depend on elaborate tools to manage code, builds and deployments. For many purposes, this is overkill. Any programmer should be able to reach for her favorite language when inspiration hits her, and Python's immediacy makes it well suited for simple automation tasks and quick prototyping. Developers usually also feel that Python gives them the headroom to move beyond a prototype without throwing away their previous work.

Indeed, Python can be used for large and complex software systems. YouTube, for instance, runs mainly on Python, and it is an oft-preferred language at organizations including Google, NASA and Industrial Light and Magic. Specialized Python libraries and frameworks exist for scientific programming, data manipulation, Web services, XML interchange and many other things.

The main disadvantage of using Python in an enterprise setting is that Python programmers can be harder to find than, say, Java developers. Python is easy to pick up for an experienced programmer, but the plethora of books, training courses and certifications in the Java world cannot be matched by Python.

Furthermore, the power and expressivity that Python offers means that it may require more skilled developers. Java or C# are more restrictive by design, forcing programmers to adhere to stricter rules around type safety and interface compliance. For some, that hinders productivity. For others, it reduces mistakes or accidents of design.

Finally, application integration concerns may dictate a certain language or platform. However, in today's service-oriented, heterogeneous systems landscape, it is entirely possible to—for example—write a Web service in Python that plugs into a Java service bus and is ultimately consumed by a Visual Basic program.

The Flying Circus

Python has a long history, but a program written for Python 1.0 still runs under the latest version, Python 2.5. New features and improvements continue to be added, following a structured proposal and review process.

In 2006, van Rossum began an effort he coyly dubbed "Python 3000." This aimed to look at how the language could be improved if absolute backwards compatibility was dropped. This effort is now coming to fruition, producing the first alpha releases of Python 3.0.

Naturally, the premise of a backwards-incompatible version of the language has caused some concern. It remains to be seen how larger Python projects manage a transition to Python 3.0, and how the rate of adoption of Python 3.0 impacts the pool of third-party libraries and frameworks.

However, Python core developers are committed to supporting existing language users. A 2.6 version of Python is due imminently, and the 2.x line is likely to continue with a 2.7 and maybe a 2.8. Conversion tools are also being made available that can analyze a Python 2.x code base and transform it for 3.0 compatibility, often without manual intervention. In the author's opinion, the Python community is large and vibrant enough to withstand some pressure, when in the long run the changes are likely to benefit everyone.

When to Consider Python

In this article, we discussed Python's strengths and weaknesses - particularly for enterprise environments. Briefly, you should consider Python when you (or your programmers):

Need a general-purpose, proven and reliable scripting language that comes with a rich standard library

Want a language that is useful across a range of programming tasks, from shell automation to Web applications

Like Python's philosophy and syntax

Find the language fun and productive.

Python may not be an appropriate choice if you:

Are building embedded or massively parallel systems for which a scripting language would be an inappropriate choice (due to concerns about execution speed)

Build primarily desktop applications, especially for Windows. Platforms like .Net usually offer more sophisticated tools and easier distribution of the final software.

Rely on teams of less-experienced programmers. These developers may benefit from the wider availability of training for languages like Java and are less likely to make mistakes with a compile-time, type-checked language.

Have specialized needs better served by other languages that you already know. For example, if you want to do a lot of text processing and you have a basement full of Perl programmers, there's no compelling reason to switch.

Martin Aspeli is a business consultant, software engineer, songwriter and émigré in London. He contributes vocally and prolifically to the Plone open-source CMS, and has written a book called Professional Plone Development (Packt, 2007). He sometimes blogs at http://martinaspeli.net.