The magazine of the Melbourne PC User Group

Python - Executable Pseudocode
Miles Strous

Python is a programming language growing rapidly in popularity - it is free, open source, and truly cross-platform (Mac, Windows, Unix, Linux, BeOS, and other operating systems, including one or two embedded systems). "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for rapid application development, as well as for use as a scripting or glue language to connect existing components together." (from the Python Reference Manual by Guido van Rossum, the original father of Python and awarded the title of Benevolent Dictator For Life (BDFL) regarding Python language development decisions). 

Python was not originally named after a large reptile, but took its name from the Monty Python television show, leading to references and humour amongst Python aficionados that may seem obscure to those unfamiliar with this background. The Usenet newsgroup comp.lang.python (cross-linked with the Python mailing list) has also developed its own whimsical folklore, such as the helpful, near infallible, and seemingly 24-hour online presence of some Python experts being regarded as "bots", software-based response agents that need no sleep. 

Python is usable in some of the same circumstances as Perl, Tcl, Scheme, or Java (or less well-known alternatives such as Lua, Ruby, and Rebol). 

It can be used as a prototyping or RAD (rapid application development) language, with the possibility of later optimisation by re-writing speed-critical portions of code in a lower-level compiled language which can then be called from Python. Moreover, with more development time made available by using Python, several algorithms can be tried for these speed-critical portions of code, which can sometimes result in optimisations many times greater than naive rewriting of the code in a faster language. A fair portion of Python code ends up as calls into the compiled C library that is part of the core Python distribution, which can often make Python sufficiently fast for a final implementation language without the need for the typical Python user to re-write any code in a compiled language. 

Python may be embedded as a scripting language in an application, or Python functionality can be extended with compiled code written in some other language, such as C or Delphi/ObjectPascal. 

An example of the use of Python as an embedded scripting or macro language may be seen in Caligari trueSpace 4.0 or later, commercial 3D modelling and animation software, or in Blender, freeware 3D modelling and animation software, or in the multimedia Magenta software It is also used for scripting real-time 3D software such as Alice or the Crystal Space game engine (available at also runs on Windows, in spite of the URL). A modified form of Python combined with a module for realtime 3D output has been used for teaching physics

Python is a general-purpose language rather than a specialised language, but a number of specialised libraries and extensions have been written for it. 

Python can be used for CGI (Common Gateway Interface) programming for Web work, although almost any language capable of understanding standard input and output can make this claim. However, Python comes with some highly useful modules that make a range of CGI programming much easier, and many large (and small) Internet programs have been written in Python as a result, including large portions of two major search engines. Google is one,, and the increasingly popular Zope web server suite

Python can be used as a glue language, passing data from one process into another - it could query an online database, perform some statistical processing on the answer, and put the results into an Excel spreadsheet. It has been used to take raw satellite data from several sources and convert it to a graphical format for another program to display or animate. It could act as a COM client, taking text from one source and using an ActiveX component to create a scrolling "ticker-tape" version of the text in Macromedia Flash format. 

It has been used for for sound and image processing, for multimedia and teleconferencing systems, and for controlling the electronic display board at the Melbourne Cricket Ground. 

As a hobbyist programmer, I personally find Python much more readable than most of its alternatives - a few unkind Python users have even likened Python to executable pseudocode and certain other languages to executable line noise (a comment that will make more sense to those of you who remember visible line noise from the days of text-based modem connections), but many Python users are quite language-tolerant and often use one of several "competing" languages depending on the task and requirements involved. 

Python makes an excellent language for teaching programming and/or object-oriented programming, and may easily be used in either a procedural or object-oriented manner. 

Python is rather unusual in that whitespace (indentation) is significant and used for block delimiting rather than specific block-delimiting characters - a feature sometimes initially disliked by programmers arriving from other languages, although some of them come to admire this as one of Python's outstanding features - it encourages, almost demands, a uniform standard of formatting and readability, which makes maintaining Python code much more pleasant. 

It does mean, however, that incorrect indentation is a syntax error by itself. 

Python can be used in a number of ways. Firstly, the interactive interpreter can be used directly (either the standard console version, or enclosed within a development environment such as IDLE or PythonWin). For instance, Listing 1 shows an example of an interactive Python session, with the user input shown in bold. You can even define functions and classes within the interactive interpreter, (see Listing 1). This is extremely useful for quick prototyping of functions and smaller classes. You can also use the interpreter to interactively test and debug a class, function, or module, while editing it in another window. 

In Listing 1 you can see the import of a maths module from the Python standard library to add further functionality to the core language, and the object-oriented manner of using this further functionality. 

Further down in Listing 1, the class that has been created and tested interactively has now been put in a separate module, which is then imported and tested while the module is still being developed. 

Figure 1. The PythonWin IDE

Figure 2. Hypotenuse calculator, Tkinter GUI version

Python comes with extensive libraries, modules, and optional functionality in the default installer, leading to one of the Python mottos : "batteries included". 

The functionality provided is extensive enough (and in most cases well documented) that feature requests are often answered along the vein of "this feature already exists and is known as such-and-such in Python" - a frequent enough response that the aforementioned folklore holds that Guido van Rossum has a time machine and uses it to sneak useful features into the language before they are originally requested. 

Python and various useful links and libraries may be found at the main website, or there is another version at that makes no changes to the Windows registry. Alternatively, a Windows-centric version may be found at, and a version implemented in Java may be found at

Numerous user modules are also available. One of the major sources for finding these is the index at The Vaults of Parnassus, by Tim Middleton, at Naturally, you are not restricted to running a program from the interactive interpreter. Once you have written a working program and saved it to a file you can run it by double-clicking on the file in Windows Explorer, dragging-and-dropping the file onto the Python executable, or you can run the file from within an IDE or programmers text editor. 

IDLE is the simple integrated development environment (IDE) included with the standard Python distribution. The editor in IDLE (written in Python) includes syntax highlighting, word completion, a class browser, popup codetips and automatic indentation, plus you can use the inbuilt Python debugger and profiler. Get PythonWin, the standard Windows extensions (written by Mark Hammond with contributions from others, available from and you also get source code folding, a COM browser, and bookmarking to the aforementioned features, as well as the COM, MFC, and other functionality provided by the Windows extensions. Figure 1 shows the PythonWin IDE with an open Python source file and an interactive interpreter session. 

Python can make use of a number of graphical interface toolkits, from platform-specific code (eg. PythonWin includes an MFC-based GUI toolkit and other Windows API modules) to general cross-platform GUI toolkits such as GTK and Qt. Two very popular GUI toolkits are wxPython and Tkinter, and the latter is included with the default standard installations for Macintosh and MS-Windows. The IDLE development environment is written using the Tkinter GUI toolkit. 

While wxPython (available at comes with a wealth of examples, a greater range of features, and is rapidly gaining popularity, Tkinter is widely used and is available on Mac systems as well as Windows/Unix/Linux. Tkinter is controlled with relatively simple code and has more developed documentation in the form of one published book (Python and Tkinter programming, by John Grayson) and one unpublished book with PDF and HTML draft versions available online (An Introduction to Tkinter, by Fredrik Lundh, at 

Listing 2 shows a quickly coded crude Tkinter GUI wrapper for our hypotenuse class, while Figure 2 shows a screenshot of the GUI at work. A bit of minor error-checking has been added with exception catching, a technique used to deal with errors that prevent normal operation but don't need to bring the program to a complete stop. 

The separation of the working machinery of our function (now wrapped in a class) and the GUI code makes it easier port functionality between various GUI toolkit. It would not be a difficult task, for instance, to rewrite this GUI snippet using wxPython for the GUI section, replacing the hypotenuse GUI class with one specific to wxPython. 

It is even possible to create a graphical interface in Delphi or Visual Basic and use Python as a COM server, using the Windows Extensions to Python, maintained and to a large extent written by Mark Hammond. Alternatively, Python may be embedded in Delphi or C/C++ code, or Pyton code can be extended with DLLs created in Delphi or C/C++. 

As well as a backend COM server, Python may also be used as a COM client. Listing 3 shows Python being used to open a text file and pass it line by line to a Microsoft Agent character for reading aloud (see Figure 3). Much of the development of this simple script was tested in interactive mode in the Python interpreter, allowing interactive control of the Microsoft Agent character. Note to anyone interested: this simple script does not check if the status of the character is clear before sending the next line to be spoken, which may be a problem with large files. 

Figure 3. Python code using COM to control a 
Microsoft Agent character

Many Windows programmers don't want to distribute the Python interpreter and their Python source code, although this is the best way to distribute Python applications. They'd prefer to distribute an executable program and support DLL files, either to protect their source code or for ease of use when dealing with end users who may not be particularly computer savvy. It is possible to distribute your Python code as a Windows executable (usually accompanied by several DLL files). 

If you have a C compiler and enough knowledge in its use, you can download the C source code to the Python core and use the freeze utility that comes with it to actually compile your Python program and any support libraries as if it were a C program. 

If you don't have a C compiler, or don't wish to go that route, you can instead download either Gordon McMillan's Installer program (available at, or Thomas Heller's py2exe (available at, neither of which requires a compiler, instead using various tricks to pack your Python source code and an adapted version of the Python interpreter engine into a single Windows executable. A number of support files and DLLs will also be required, and will be gathered by these programs into a folder which can either be distributed as a ZIP file or packed into a nice setup/installation executable using one of the various installation programs available as freeware, shareware, or boxed commercial software. 

If you have used some of the more complex third-party Python add-ons available (such as PythonWare's Python Imaging Library, for image manipulation, available at, please read the documentation of these executable-creating programs carefully or you may have trouble creating a successful executable. 

Next month, I hope to take a brief look at a few simple CGI/web uses for Python.

Reprinted from the August 2001 issue of PC Update, the magazine of Melbourne PC User Group, Australia