Read an Excerpt
Chapter 1: So What's Python?
"And Now for Something Completely Different.."This book is about using Python, a public-domain programming language. In acronyms, Python is both a very-high-level language (VHLL), and an object-oriented dynamic language (OODL). As a preliminary definition, Python can be described as a new kind of language tool. For many users, its
- Support for object-oriented development
- Powerful programming constructs
- Extendible and embeddable architecture
- Remarkably clear syntax and coherent design
make it almost ideal as both a scripting interface for modern systems, and a stand-alone rapid- development language. For example, Python's object-oriented nature mixes well with frameworks written in C++. And as a standalone tool, Python is commonly used in domains such as system administration, graphical user interfaces, internet scripting, and database programming. We'll refine this description in a moment.
The Life of PythonPython was invented around 1990 by Guido van Rossum, at CWI in Amsterdam. Despite the reptiles, it's named after the BBC comedy series Monty Python's Flying Circus. Guido was also involved with the Amoeba distributed operating system and the ABC language. in fact, the original motivation for Python was to create an advanced scripting language for the Amoeba system.
But Python's design turned out to be general enough for a wide variety of domains. It's now used by a growing number of engineers around the world, in increasingly diverse roles. For instance, a number of companies use Python in commercial products, for tasks such as GUI development tools, WWW scripting, interactive television, rapid program development, on-site customization of C++ class libraries, and more.
Since it first appeared on the public, domain scene in 1991, Python has continued to attract a loyal following, and spawned a dedicated Internet newsgroup, comp.lang.python, in 1994. And as this book was being written, Python's homepage debuted on the WWW at http://www.python.org/.
To help manage Python's growth, an informal organization aimed at supporting Python developers has begun taking shape: the Python Software Activity, or PSA for short. Despite its public domain status, Python is a well-supported system, thanks to the dedication of its inventor and the Python community. For example, the PSA facilitates Python workshops, and maintains a software and resources locator service.
"Buses considered harmful"The PSA was originally formed in response to a thread on the Python newsgroup, which posed the semiserious question: "What would happen if Guido was hit by a bus?" Guido van Rossum still manages most new developments in Python, but the PSA and Python's user-base help support the language, work on extensions, etc. Given Python's popularity, and the PSA infrastructure, bus attacks seem less threatening now; of course I can't speak for Guido ...
Finally, Python is true freeware: there are no restrictions on copying it, or distributing it with your products. it comes with complete source code, a debugger and profiler, built-in interfaces to common external services, plus tools for adding other interfaces. System functions, GUls, and databases are supported "out of the box." Python programs run on most platforms, including nearly all flavors of UNIX, PCs (DOS, Windows, OS/2), the Macintosh, and others. And by the time you read this, Python should be part of most Linux distributions.
What's All the Excitement About?Back to our description. Python has been called a "next-generation scripting language." This definition probably summarizes the language's distinctions, and the theme of this book, better than most. Although different people like different things about Python, there are some common reasons underlying its popularity. Some of the central points in the "Python philosophy" are:
- A scripting language doesn't have to be hard to read, write, and maintain. Issues of aesthetics and readability need not be sacrificed in the interest of utility. With the right tool, there's no reason to abandon normal standards of quality, even for "quick and dirty" code.
- An extension language doesn't have to have limited functionality. The design goals of embeddability and semantic power aren't necessarily contradictory: an extension tool can also be a full-featured programming language
- A dynamic language can be used for more than trivial tasks. There's no reason that a language can't both provide rapid response during the development cycle and also have features that make it useful for building more advanced systems.
- Object-oriented programming can be a useful paradigm, given the right tool. When easy to apply, OOP can be a powerful tool for structuring and reusing code. An object- oriented language doesn't necessarily also have to be complex or difficult to use.
- No language is an island. By providing both a powerful dynamic language, and well- defined interfaces to other languages, Python fosters hybrid systems that simultaneously leverage the rapid turnaround of Python, and the efficiency of C.
Python's integration support is a crucial property: as we'll see, much of Python's power comes from its open design, and its interfaces to external services. In fact, some consider Python's library of existing interfaces to be among its greatest assets. As we'll see, embedding APIs in a high-level language like Python makes them easier to use. Moreover, Python's integration tools make it practical to embed Python in products, and to apply paradigms such as rapid prototyping and rapid development.
But compared to other public-domain scripting languages, the first two points here- coherence and semantic power-may be Python's biggest distinctions. Aesthetic issues such as readability and design coherence are always hard to define, but crucial in a programming tool. As one Python user put it,
"Python looks like it was designed, not accumulated."Python's inventor has done a extraordinary job of balancing the goals of simplicity and utility. We'll see that Python's clear syntax and high-level tools encourage the creation of easy-to-read, reusable software.
We'll also find that Python programs tend to resemble traditional languages such as C and Pascal, rather than scripting languages like Perl or Tcl. In fact, Python is something of a scripting language in the guise of a traditional language. Another observer summarized this fusion of ideas well:
"Python bridges the gap between scripting languages and C."By providing a full-featured programming language and supporting modern development paradigms, Python brings programming tools used for more substantial systems to the scripting world. For example, C is poor for fast prototyping, and awk is almost useless for designing large systems, but Python does both well. In short, Python is a simple but powerful language, suitable both for "quick and dirty" scripts and medium-to-large-scale systems development.
Is it a "scripting language" or an "extension language"?The terms "scripting language" and "extension language" are often used interchangeably to refer to an embedded, interpreted language component. Unfortunately, "scripting language" sometimes denotes system administration languages, used for writing shell tools. Since Python can be used in both roles, we'll use both terms too. But the term "scripting" isn't meant to imply that Python is just a shell tools language. For instance, we'll see that embedded Python code can take many forms: character strings, objects in module files, executable script files, and more.