AUUG: Victorian Chapter - Chapter Technical Conference




Python: Yet Another Object Oriented Interpretive Scripting Language

Enno Davids, Metva P/L.
Enno.Davids@metva.com.au






ABSTRACT

Many programming languages have been created for many varied reasons. One of the more recent arrivals is Python, a language which seemingly offers some of the features of Awk, Perl and various shells, with the speed and interpreted virtual machine of Java. Like is other recent cousins it embodies OO concepts and has adopted most of the other currently fashionable programming metaphors. This then, is an overview of what Python offers and why you might consider embarking on the learning curve for "Yet Another Object Oriented Interpretive Scripting Language"


Why Python

Lots of computer languages come and go all the time. Some are flash in the pan. Some are tailored to specific environments, some to specific hardware or specific types of problems and some are completely general purpose. Unless we happen to find ourselves in one of the mooted restricted problem domains for which the specialist language was invented, the general purpose languages will be of the most interest to us.

Python, is such a general purpose language. Still, we must ask ourselves if Python brings anything special to the table. It is certainly true that it is a general purpose and like most modern languages it has adopted most of the popular programming metaphors which have developed in the past decade. Also in common with the more modern languages Python jettisons some of the older features which have outlived their usefulness. This though is not sufficient reason to go to the significant effort of acquiring a new language. So the question becomes, what does Python offer we can't find elsewhere.


Features

Python has lots of features worth discussing.

This feature list only scratches the surface. Its also worth noting that as with much other Freeware software from the net, Python is very much a work in progress and this means that by the time you read this, reality may in fact have moved on from where it was when I wrote this. It may also have shot off at a tangent, so "caveat emptor".


Overview

Rather than attempt a full blown tutorial for the language, I'd like to just hit a few of the high points. If you want a good tutorial introduction to Python , several now exist on the Web, the source package naturally contains documentation (in addition to the reading the source!) and two books have hit the shops to help you along.[1][2] (In fact, for people with foreign language skills, there is at least one book on Python published in German in addition to those listed here.)

The core of the Python language is the same ALGOL inspired procedural syntax and expression formation that has featured in most languages since then. Block structure as has been mentioned, is defined by the indentation of the source code which makes Python programs look quite unlike most other modern languages when first seen. The style of these indents is not significant as any white space is accepted, only equal amounts are counted off to determine equal block scope.

Python features many data structures not seen in more traditional computer languages. Some of these are familiar to us from awk and perl like associative arrays (hash tables to the traditionalists) called Dictionaries in Python and Lists, which are simply ordered array-like objects. Where Python differs from awk and perl is in the manipulation of these objects which in the OO style are done through intrinsic functions, which is Python nomenclature object defined access methods. Like Java, packaging the access and manipulation functions with the objects they access and manipulate makes namespace management easier, and allows greater clarity in the code you write.

Ranges (i.e. lists or virtual lists of integers) are also featured in our list of unique Python types. Python also has a number of composites of various sorts which can be treated as types as classes as structures are in other languages. Examples here are the Traceback, the Module and the File. This latter is the hook by the C standard I/O functions have been re-cast in Python.

Lets look at some code to give you the flavor of what we're talking about.


	class Person:
		def __init__(self, name = '', job = '', pay = 0):
			self.name = name
			self.job = job
			self.pay = pay

		def __getattr__(self, name):
			if name =='tax':
				return self.pay * .30
			else:
				raise AttributeError

		def __repr__(self):
			return 'Person |%s|' % self.name

		def info(self):
			return self.name, self.job, self.pay, self.tax
Here is a reasonably simple code fragment which serves to illustrate what some Python code looks like. Note the lack of delimiters for the block structure. This is all OK until we get to the end of a block where Python has a slightly 'dangly & unfinished' feel when you first see it. The feeling passes, for what its worth.

Its also worth noting that the function names which start and end with double '_' characters are all overriding builtin (inherited) functions of the same names provided by Python itself. The __repr__ function provides a human readable representation of a Python object. This is analogous to the toString() functionality of Java. The __getattr__ function is used whenever a member variable is accessed in Python. In this case, we are trapping accesses to a member called 'tax' which you will note we have not defined, which allows us to access self.tax for instance at the end as if it was any other member variable while not needing real storage for it.

The example here, shows a both the syntax and a little bit of the Python philosophy. The overriding of inherited abilities as shown above is a simple and elegant mechanism for providing basic functions to the programmer. Nevertheless, it may not be to your taste.

Another feature worthy of mention is one which is more familiar to programmers who have worked with OO languages before. In Python these are the intrinsic functions (methods in most other OO languages). These functions allow the user to perform operations on the object for which they are defined. In the case of lists for example some of the following operations are common.


	L = range(5)			create a list
	L.append(-3)			add an element to the end of the list
	L.insert(3, 50)			insert an element at index 3
	L.sort()			sort the list
	del L[4:5]			delete a slice (section) of list
	L.reverse()			reverse the list
The benefits of this quickly become apparent after only a short period of use. The operations which are bound to an object are all reasonably clear (although inheritance often means that you can do more things to an object than you expected) and the type checking is less of an issue as the operations are bound to the type intrinsically. Its also true that strings of characters share some of characteristics of lists and to a limited extent can be manipulated in the same manner. (It must be noted that strings are more immutable than their 'C' counterparts but have much in common with the Java String class).

Another concept recently return to favor is that of exceptions. Python has exceptions and they look and behave much like the exceptions of C++ and Java (and soon to be C). The basic format is shown below:


	raise SpamError			how to raise an exception
handling the errors is then done in the following manner.


	try:
		... a bunch of statements ...
	except error, details:
		... exception handling statements ...
	else:
		... no exception raised ...
Note that Python also supports a 'finally' clause as a catchall for code that must run whether an exception or non-exception exit is being followed.

Most of the rest of the language is unremarkable. The usual control constructs are all available albeit with a slightly new look about them. Expression evaluation is also a little more relaxed than it is in some other languages.


	a = b = c = 0			multiple assignments
	[a, b] = ['a', 'b']		multiple different assignments
	w < x < y < z			multiple range testing
Finally, a quick word on the builtin functions which Python makes available. These are for the most part not worth dwelling on in detail. Each of the references had ample documentation on each of these functions. Here we merely list them to give a flavor of the facilities.


	abs		execfile	locals		reload		
	apply		filter		long		repr		
	callable	float		map		round		
	chr		getattr		max		setattr		
	cmp		globals 	min		str		
	coerce		hasattr		oct		tuple		
	compile		hash		open		type		
	delattr		hex		ord		vars		
	dir		id		pow		xrange		
	divmod		input		range		
	eval		int		raw_input		
	exec		len		reduce		

Libraries

This brings us to the libraries which ship with Python. Like Java, Python comes with a rich set of libraries. In fact, where Java is choosing to blaze new trails for some of its new functions (e.g. object serialization, RMI), Python has chosen to implement the old interfaces with which we are already familiar and where services may already exist (e.g. XDR/RPC, .

Some of these libraries are crucial to the flexibility which Python offers the programmer. In fact, much of Python's appeal may lie in not in the any esoteric features of the language, but rather, the fact that so much work has already been done by others and made available for us to build on. So, lets look at what some of the libraries are:


GUI

The GUI API provided by Python is based on the Tk toolkit as is currently common in the UNIX freeware community. This means that those already familiar with tcl/Tk will have a level of familiarity with the way Python does things. While Tk is not as verbose as some Windows interfaces, it does share the trait of being not at all pretty to work in.


	from Tkinter import *

	win = Frame()
	win.pack()
	Label(win, text='Hello World').pack(side=TOP)
	Button(win, text='Quit', command=win.quit).pack(side=BOTTOM)

	win.mainloop()
The code fragment above produces the output shown at the right. This is of course a laudably terse example of the "Hello World" but will not be setting the world on fire any time soon.

For those seeking an alternative, Python also offers the C STDWIN package which has been around for some time. Whether this is a step up, down or sideways is left as an exercise for the reader.


Networking

There is laudably little to say about Python's networking. It is in every respect a 'plain vanilla' wrapper around the BSD defined socket interface. Unlike Java, Python supports the select() semantics of this interface too, allowing a more efficient coding model than the excessively thread reliant Java approach.

Python has also as we have alluded earlier, a number of complex protocol implementations available over the top of this low-level access to the net. These make light work of much of the sort of network interaction which we wish to do most commonly. Of particular note are the Web based protocols and the support for manipulating the data which might be retrieved using them. It is interesting to note that the Infoseek Web Search engine is purported to be implemented in Python and I presume that the some of Python's abilities in this regard have fed off that development and perhaps vice versa.

Also of note, Python has good support for demangling the rather bizarre and more than a little rococo structure of the CGI interface presented by the mainstream HTTP servers. This means that Python is a good fit for building that level of Web infrastructure too.


Grail and Plugins

You will have noted a tendency to compare Python to Java throughout this presentation. Some of this is simply because they are both (from my perspective at least) the new kids on the block. The other reason is Grail.

Having provided a rich set of protocol support and the ability to manipulate HTML, SGML, URL's, images and sound, the temptation to take the final step and build a complete Web browser around these pieces must not merely be great but be unsurprising.

Grail, is the product of this effort. Grail is a Web browser written entirely in Python just as HotJava was a browser written entirely in Java. Like its commercial counterpart, having an interpretive engine at its lowest level means that given support for downloading content from a Web server, the Grail browser can then allow that content to run and provide the same sorts of dynamic content which Java is currently being used for. Unlike Java, with its strong commitment to security, such downloaded Python has more questionable restrictions placed on it. This is clearly a place where you will wish to tread carefully.

In spite of this, Grail has other things to offer a potential user. Like Python, Grail is free and we are told is easily extended to offer new functionality. This may be quite useful in intranets where a browser interface can handle much of the corporate data but some extra facilities are required. For example, a medical imaging system could have fixed data on screen, live data coming in to a Python applet and have an extra interface to control a live video window with say real-time data from a 3D imaging system.


Embedding Python

Python like tcl and perl offers a clean interface to allow it it to be embedded inside other programs. This then allows Python to serve as a scripting language to control features of those other programs. Taking the medical imaging example above an embedded Python interpreter might serve as the control mechanism for the 3d data capture device and a server written in Python and executing in the data capture device might be used to provide the real time data to the Grail hosted Python applets which were mooted to be displaying such data.

The key here is that the Python interpreter may be wired into any other program so that it either runs a whole Python application or issues a command prompt or calls into a specific function in a Python module to perform a specific service. This latter method is provided as a set of changes to the popular Apache HTTP daemon which allows it to execute Python programs (which are presumed to provide HTML for the server to send to the client).


Extending Python

A second part of the interface between the Python interpreter and other compiled modules is the extension mechanism Python provides. This in fact is where the python interpreter calls down into an object module. As it happens the Python interpreter is itself built using these same primitives which means both that they get pretty well tested and that most of the interfaces we might like to change are well exposed to the mechanism we expect to use already.

Extending python can be attractive for a number of reasons. The first is the extra hardware discussion we had above. If we want to drive some new hardware, the chances are we will need some special code to open the device, and manipulate it. This is a place where a small extension module can pay big dividends. The other occasion where you might be tempted to code an extension is where you have a built a Python application, profiled it and found a large hotspot with the profiling tools. By recoding the offending code in C and extending the Python interpreter this way, you may make the solution of your problem much more efficient.


Futures

There are a short list of things which will be added to the core Python before too long. Matrix objects and Complex numbers are expected to round out the abilities of the language and will complement the existing arbitrary precision integer arithmetic.

Some of the other extensions are less mainstream and hence will garner less support and be less devastating if they fail to arrive or be useful. These mooted APIs are:


More Information & Sources

The two best places to look for Python and Python related information are undoubtedly the Python Web site and the Python FTP site. The are as is the style:

The next best place to find things is either of the books mentioned in the bibliography. Each comes with a CDROM packed full of bytes and you get a book for free! Well, sort of. Each of the Books has its pros and cons and one or the other may well be more to your tastes than it counterpart. For German speakers, there is in fact a German text on Python also available. Details are available on the Web site although you may well need to order a copy sight unseen.

Finally, the comp.lang.python newsgroup is a good forum for discussing language features. As ever though, it not likely to be wise to go there asking for beginners tutorial instruction. Like most newsgroups they like to think you've made some effort yourself first before bothering the entire world at once.


Bibliography






Prev Paper

Conference
Programme

Next Paper



Author's papers