This language can handle simple scripting jobs or build large OOP libraries.
Jeffrey P. Shell
py*thon \
|
pi-
|
thän\
n
(1836)
1
: a large constricting snake
2
cap
: short for the British comedy troupe "Monty Python's Flying Circus"
3
cap
: a powerful modern object-oriented-programming (OOP) language
Python, the third definition in the fictional dictionary entry, is an ideal language for many of today's computing tasks. It's been used to build client/server databases and Unix system administration tools, create Common Gateway Interface (CGI) sc
ripts, embed objects, and facilitate rapid applications prototyping. Its
dynamic range
and variety of hooks make it suitable across many boundaries where one programming language halts and another is needed. Incidentally, the name Python derives from the second dictionary definition, not the constricting snake of definition one -- although as a programming language, Python is able to "coil" itself around a variety of platforms. That's because, like many of the new programming languages today, Python compiles to neutral byte code.
Unlike Java, however, this compilation happens on the fly, as modules are imported into the Python interpreter. The interpreter performs automatic version checks against the source code file's modification date and fetches the most recently generated byte-code file. The version-control process is almost invisible to the programmer and is evident only when a triggered compilation slows the launching of a program.
Python offers strong interplatform operabi
lity. A majority of Python programs written on one platform can run effortlessly on another. Python implementations exist for almost all flavors of Unix, as well as for the Mac OS; Windows 3.1, 95, and NT; OS/2; the BeOS; and NextStep. Even though you can write Python programs specifically to features of each GUI, Tkinter has been adopted as the language's
standard GUI
. Tkinter (for Tk interface) in turn uses Tk, which is Sun Microsystems' graphics toolkit. Tk is typically operated by Sun's Tool Command Language (TCL) but is in fact usable by other scripting languages such as Python and Perl.
Python Features
Python got its start through its powerful scripting features. It is often presented as a bridge between Unix shell programming and C programming. That is, Python is ideal for projects that are too complex for the normal shell tools to handle, but not so complex that they are worth writing in C or C++. Because Python is a very high-level language, it provides
the programmer with complex data types unavailable in shell scripts or programming languages, such as dictionaries (associative arrays). System-administration scripts, mailing-list automation, and other sophisticated tasks can often be written using relatively little code.
Another of Python's strengths is that it is scalable. Python programs can be short shell scripts or simple function-based programs. They can also be full-blown object-oriented applications handling large jobs. This tremendous scalability allows Python to be adapted to any number of tasks where you would normally use two or three languages to craft a solution. Because Python's capabilities range from the simple to the sophisticated, what might start out as a simple Python script can blossom into a large-scale module of object classes, while maintaining the same code simplicity.
Despite its power and range, Python is an easy language to use. One of the major influences on Python's appearance is a smaller language called ABC. ABC was d
esigned as a more modern BASIC, providing some high-level datatypes in a small, robust language. Like BASIC, ABC was constructed as a teaching language and has many nice touches for programming simplicity. These same features appear in Python. For example, Python has no
begin
and
end
statements or braces. Code grouping is done through indentation, and the Newline character acts as a command separator. A command can sprawl over many lines if necessary, or multiple commands can fit on one line. This format practically forces good programming style, which in turn produces neat, maintainable code while conserving space. Most seasoned programmers converting to Python are surprised by this at first, but they soon grow used to the freedom it brings. See
"Operator Overloading in Python"
for an example.
Python is an easily extendable language. The core of Python is written in C, and the source code is freely available, along with a complete API for extending the language. Th
is lets you boost the speed of a commonly used function or hook Python byte-code files to large, already-made binaries such as imaging libraries or to platform-specific APIs like Microsoft Foundation Classes (MFC) or the Mac OS Toolbox.
Having grown up in the age of the Internet, Python comes standard with many modules that implement objects for use with most TCP protocols. These Internet-savvy modules enable Python to implement easy Website maintenance and administration programs, and they allow you to write smart agents and implement many other server tasks.
Python can be both server and client. On the server side, Python can process HTTP requests, filter incoming messages and mail, access large databases, perform as a CGI script, be embedded in Hypertext Markup Language (HTML) if the server allows it, or serve up its own objects. On the client side (especially through use of Tkinter), Python can be a client for any TCP format, including HTML. It can also transmit objects over the network to other P
ython servers/clients or exist as a platform-neutral database front end, which makes it a great intranet asset.
Because of its scriptlike nature, Python is a natural as an embedded language inside large compiled programs such as databases, multimedia applications, groupware environments, virtual worlds (both text and 3-D), and HTML documents.
In terms of security and today's concern with distributed Internet applications, Java definitely excels. Because Python is more of a scripting language, it can have lots of freedom with the local file system, which creates security risks. Furthermore, Python allows many ways to dynamically invoke commands read from any file type, even a TCP socket. Python does have a restricted mode that "fakes" many of the standard functions and modules but actually uses only those deemed "safe." If an attempt is made by the code to access restricted material, an error is raised and the user is alerted.
For a programming language, Python is flexible. Classes and method refer
ences in Python are treated as first-class objects. That is, new methods and member variables can be added to a class at any time, and all existing and future instances of classes are affected by these changes. This way, a scheduled event on a server program can change a variable in the class definition that defines each user's privileges. Thus, when standard office hours end, access could be broadened automatically to certain users with a single line of code such as
userClass.restrictions=3
. All existing and future instances of
userClass
are updated and use this new value until the class variable is changed again. A programmer maintaining the code for the server could log in and be allowed to add or update classes and methods without having to take the server down.
Python is also a language of "nothing but hooks." A programmer has hooks into almost every aspect of the system and can read or overload these attributes to further customize their code.
Ready to Slither
The standard distributions of Python have over 120 ready-made modules, including Internet libraries, cryptography, various DBM implementations, regular expression and string handling, text formatting, math, file handling (including full Posix file handling), code profiling and debugging, object persistence and storing, and Tkinter. Often included in these distributions are many demonstrations of Python's use for client/server applications, Web serving, and embedding. Also available are free modules for use with SQL and other databases, and an extended version of Python called Numerical Python, which allows for fast, high-level math constructs. The Mac OS version includes support for the Mac Toolbox andApple Events. It can create AppleScript-style applets on CFM-capable and PowerPC machines. The Windows version allows use of MFC and has a Netscape plug-in to run Python applets. An ActiveX/Explorer interface and an OpenDoc container are currently in the works. Support for Silicon Graphics' GL and Sun's Aud
io Device is also standard, making Python ready to use right out of the box.
Python is available completely as freeware, made possible by contributions to the Python Software Activity (PSA), a legitimate organization whose proceeds support Python financially. A growing fleet of volunteers and special-interest groups (SIGs) help in maintaining Python code and extending it into new platforms and fields, making Python a nearly universal solution to most programming problems. You can access the Internet at
http://www.python.org
and comp.lang.python for complete information about Python.
A simple class with operator overloading example in Python.
__add__(self,other) overloads the + symbol when an instance
of addSquare is on the left. __radd_
_ overloads the + symbol
when the instance is on the right. The __add__ method checks
the other object being added, and if it's a string, it
converts it to a floating point. The class adds the squares
of the two objects. (the "__" denotes special methods).
import string
class addSquare:
def __init__(self,value=1):
self.value = value
def __add__(self,other):
if type(other) ==
type("abc"):
other = string.atof(other)
return self.value**2 +
other**2
__radd__ = __add__
* Cross-platform:
It uses Sun's Tkinter as the GUI and generates
machine-indepedent byte code.
* Scalable:
It can implement simple shell scripts or build complex
object-oriented libraries.
* Extendable:
It can hook directly into other binaries such as the Microsoft
Foundation Classes (MFC) library or the Mac OS Toolbox.
* Embeddable:
Because of its script
like nature, it can be embedded in
HTML, groupware environments, and databases.
screen_link (26 Kbytes)

Python uses Sun's standard Tkinter GUI so that programs can execute on many platforms.
Jeffrey P. Shell is in charge of object technologies and Python programming at Cynapses. He can be reached at
jeff@cynapses.com
.