|
Development Tools
|
Python, anyone?
You may have dismissed Python as just another scripting language
beginning with 'P,' but more and more open source developers are finding
Python has some bite
Summary
At first, the Python scripting language may sound like another
version of Perl, but Robert Richardson highlights the differences that
make it useful in its own right.
(1,500 words)
By Robert Richardson
|
 |
hile
the rest of the world has been catching on to the Perl
scripting language, the Linux
community, long since past the pleasing shock of Perl's power, has
been catching on to a
different scripting animal -- Python.
If you just flatly state what Python is -- a scripting language with
open source, a
relatively small learning curve, and lots more power than most older
scripting environments
-- it sounds suspiciously like Perl. "Do we need another Perl?" you
might ask.
But there are differences, and the more I've looked at Python and
talked to the people who
use it, the more it's clear there's plenty of room for both Perl and
Python to grow and
flourish.
What's it like?
A quick look at Python's syntax should give us some indication of why
Python is different, as well as when and where we might use it.
For those coming from C, Java, or Perl backgrounds, the thing that
jumps off literally any page of Python code is that the code looks
vaguely "naked." The ends of statements go without terminating
semicolons. There are no curly braces wrapped around blocks of
statements.
Instead, statements are terminated merely by line endings, and blocks
are set off simply by dint of consistent indentation. So an if-then
statement looks like this:
x = 3
if x == 4:
result = x + 2
print x
else:
print 'Try again.'
Guido van Rossum, the fellow who originally concocted Python, says,
"Python's intention is to cater to infrequent users," as well as to
full-time Python users. With infrequent use, he points out, a "rich
syntax is more of a burden than a help."
To keep things simple, Van Rossum made Python "more regular," with
"fewer 'shortcuts' that the casual user is likely to forget. Python
emphasizes the ease of reading code -- one's own, or another
programmer's -- over writing."
Though Python may not offer abundant syntactical shortcuts, many
"Pythonistas" liken the spare, punctuation-light code to "pseudocode
that actually runs." There are definitely plenty of instances where
this claim holds, as in this bit of code that performs an FTP session:
from ftplib import FTP
ftp = FTP('ftp.someserver.com') # connect to host
ftp.login() # login anonymous
ftp.cwd('pub/samples') # change directory
ftp.retrlines('LIST') # list the directory
F = open('current_issue', 'w')
ftp.retrbinary('RETR current_issue', F.write, 1024)
ftp.quit()
Still, that's not to say that Python doesn't wind up looking pretty
obscure sometimes, particularly owing to the way it supports object
orientation to its core.
Certainly, Python does plenty of old-fashioned things (i.e., it functions
in familiar, old-fashioned ways), but it also lets (indeed, pretty much
requires that) you treat most of your creations as object
classes. This means things get self-referential in a hurry,
and you wind up with code that's just line after line of
self.this and self.that.
Which brings us to an important distinction between Perl and Python:
Perl springs more or less directly from Larry Wall's
desire to get some text scanning and twiddling done quickly and
painlessly. That's why Perl has great support for regular expressions
(and why they're built directly into the language), for grabbing input
from groups of files, and for kicking out reports.
Python, on the other hand, comes more from Guido van Rossum's desire
to collect the best
features he'd worked with as a computer sciences academic and roll
them all into one language
with a clean, consistent syntax.
Mark Lutz, author of Programming Python (O'Reilly &
Associates, ISBN 1-56592-197-6), says Perl "tends to be best
for 'lone wolf' development, where one
programmer writes code that others (and perhaps she) will never need
to read or maintain." Perl's core premise, that there should be multiple ways
to do everything, "makes it fine for write-once, throwaway coding tasks,
but much less than ideal in more substantial and realistic development
scenarios."
Life of Python
Guido van Rossum began work on Python in December 1989, as a project to
keep him busy during the winter holidays when his office would be closed. He
wanted to write a successor to ABC, a teaching language he'd helped create that had languished unappreciated in the academic world.
For this new language, he wanted to provide direct appeal to Unix
users who were accustomed to programming in C; he also intended to make Python easy to connect to other languages and development environments, unlike the closed environment found in ABC.
His creation borrows a certain amount of its approach from Modula,
with a few flourishes (such as dynamic typing and object orientation) coming from classic comp-sci languages like Lisp and Smalltalk. The Python interpreter, which
performs a fast compile to bytecodes and then executes them, is written in C.
Like other flourishing languages, Python offers
- Object-oriented classes
- Exceptions
- Dynamic data types
- A frightening array of interfaces to system calls, external
libraries, and several windowing systems (including Tk, X11, Motif, Mac, Windows, and MFC)
By now, almost 10 years since its creation, Python runs on plenty of
environments other than Linux and Unix (it was originally conceived on a Macintosh, in fact). There are versions for Windows, DOS, OS/2, Macintosh, and Amiga. The Python license allows its use in proprietary software, unlike software protected by the GNU GPL. The GPL normally does not permit the use of software copyrighted under it in nonfree software. Neither the Python license nor the GPL restrict software from
commercial use. There's a
copyright on Python, but you're pretty much free to do with it what
you will.
For just plain scanning a file and replacing every third character
with a dollar sign, you'll need a few more lines in Python than you will in Perl, plus you'll probably find that Perl executes such work somewhat faster. But if you're interested in rapidly prototyping a program you might otherwise have written in C, C++, or Java, you'll find that Python more naturally supports the constructs you're working with, all the while keeping to its script
nature by letting you get a lot of work done in relatively few and straightforward statements.
Faster work
Indeed, Python can be a great tool for getting applications written
quickly. This is in part because it's a language that gets a lot done with relatively few lines.
It's possible to write large applications in Python and, where speed
of execution isn't a primary factor, it's possible to simply package the Python and call it a day. This packaging can happen in a number of ways.
There's a utility that will transform your Python code into
compilable C code, or you can "freeze" the Python code into an executable that stores and runs the Python bytecode.
Perhaps the most interesting option of all lies in a recent addition
to the Python pantheon -- a version of Python, called JPython, that runs atop the Java virtual machine.
With JPython you can work on a project interactively, literally
testing changes as you make them, then save your project as Java bytecode that will run anywhere Java does. In this environment, your Python is extended to give you easy access to all the basic Java classes, so you can use the same AWT windowing environment that you'd use in Java to get your widgets onscreen.
I asked Van Rossum what Python projects he's been working on lately.
He had a big answer and a small answer.
"At the large end," Van Rossum says, "I'm writing an extensible
integrated development environment for Python called IDLE. It uses Tkinter -- Python's de-facto GUI standard based on Tcl/Tk -- for the GUI interface and thus it is portable between Unix, Mac, and Windows."
The project, he says is "medium sized," with some 40 files and 5,000
lines of code. He adds, "It's a joy to see how easy it is to write in Python. Clearly, Python is not just for 'scripting.' "
"At the small end of the spectrum," Van Rossum says, "I just rewrote
a small C program to save audio data directly from CD-ROM."
Anybody else?
For all its strengths, Python has been overlooked in the floodlight
glare surrounding Perl,
which has flourished of late largely because so many Unix hackers
use it to write CGI scripts
for Web servers. But don't accept the impression that Python never
gets used.
Several major Web sites, including InfoSeek and Four11, are supportive
users. Other corporations are closet users, visible only when they goof
in their code writing and get debugging "traceback" messages from their
scripts. These tracebacks are displayed in your Web browser (which, by
the way, brings up the positive point that Python checks segment
boundaries and the like very closely, and Van Rossum guarantees that
Python never just bombs -- you always get a traceback). The most recent
traceback "outing" I've heard of was at the American Greetings site
(see Resources).
Finally, yes, it is named after Monty Python, not after the snake.
Making references to Monty Python in source code examples seems to be
very nearly required in the otherwise fairly sedate and serious Python
community.
So, to paraphrase Monty Python troupe member Eric Idle, one might well
come to the conclusion that if you have only as much fun using Python
as Guido had writing it, then Guido had twice as much fun as you.
Discuss this article in the LinuxWorld forums
([an error occurred while processing this directive] postings)
(Read our forums FAQ to learn more.)
Correction
In the original text of this article, we said the following about Python: "It also has a more
open licensing arrangement than GNU provides because it doesn't impose any
restrictions on commercial use."
That statement was wrong in two respects: First, the standard GNU license does
not impose any restriction on commercial use, only on proprietary (nonfree)
use. Second, while the use of software copyrighted under the GPL in proprietary is
software generally prohibited, there is an exception even
to this. Guile, the GNU extensibility package, contains public domain software
which may be incorporated into either free or proprietary software without
restriction. Python's license also allows its use in proprietary software
Our mistake was in confusing the terms commercial use and proprietary
(nonfree) use. GNU software may be used in commercial software -- that is to
say, in software that is offered for sale. What is not normally allowed under
the GNU GPL is its reuse in proprietary software -- that is, software whose use
is not protected by the GPL.
The section in question now reads:
"The Python license allows its use in proprietary software, unlike software
protected by the GNU GPL. The GPL normally does not permit its use in
nonfree software. Neither the Python license nor the GPL restrict software from
commercial use."
We apologize for having gotten it wrong. Thanks to Nick Moffitt and RMS for
notifying us of this error.
|
|
 |
About the author
Robert Richardson's articles have appeared in magazines such as
Network Magazine, BYTE, Win32 Scripting Journal, and Internet Business. His free Small Office TECH newsletter is available at
http://www.smallofficetech.com/.
|
|