Python Paper
Home Projects Papers Opensource Infolets Seniorproject

Why Study Python?

Over the past few years I have  written several programs in Java™.  I have gotten a good feel for both the syntax and the object model of Java™. Java™ is a great language for application development, but most of the programs that I have written are small single function programs.  When I write my programs I don't want to spent a lot of time thinking about the design of my program.  The concept of "throw away code" or rapid prototyping suits my needs better. 

There are many scripting languages around today that could possibly suit my needs. Perl is the one that immediately comes to mind.  I have written a Perl script or two.  Perl is undoubtedly a very powerful language, but the extreme flexibility of Perl (every problem could be solved 10 different ways) rubs me the wrong way. 

Enters Python.  Everything about Python seemed to fit well with my needs.  The language was designed with the idea of rapid prototyping in mind. Python is completely object oriented, and with the creation of JPython I can capitalize and integrate my knowledge of Java™ with my knowledge of Python.

Java and Python

I have only started studying Python recently.  Therefore in my approach to describing Python I will attempt to compare it to Java™. 

Java and Python support many of the same features, in other ways they are complimentary.  In this report I will attempt to just give a general overview of Python (Hopefully just enough to wet your appetite and encourage you to explore the language further on your own). 

The best intermediate book on Java™ that I have read is Thinking in Java by Bruce Eckel ( the book can be downloaded for free at http://www.bruceeckel.com).  If you have not read the book it is worth a look.  The reason that I liked the book so much is that it took a software engineer's approach to programming.  I have yet to find a Python book that takes a similar approach,  but the tutorials and documentation at http://www.python.org are top notch so that is a good place to start your Python education. 

What is Python?

Python is an interpreted, interactive, object-oriented programming language. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as well as to various windowing systems (X11, Motif, Tk, Mac, MFC). 

The Python interpreter is written in C so basically any platform that has a C compiler can run Python (Can anybody say "Write once run anywhere"™).  Today Python can run on all flavors of Unix, Linux, DOS, Win16, Win32, Macintosh, OS/2, Amiga, BeOS, QNX, VMS, and Psion. 

Perhaps the most unique feature of Python is that white space is important.  If you have programmed in C, C++, or Java you know that how you choose to indent and format your program is mostly an issue of personal style.  This is not so in Python.  There are no curly braces in Python.  Python is able to tell where a block begins and ends based on the indentation of the blocks.  Although this feature seemed to fit in well with Python's idea of writing as little code as possible, it seemed like an unnecessary burden to me until I went and looked at the Open Source  Zope™ Server (http://www.zope.org) which is written almost exclusively in Python .  What I discovered is that by forcing consistent formatting structure it made it much easier for me to pick up someone else's source code and read it. The more I have to read other peoples code the more I understand the importance of good readability. 

A brief overview of Python's Features

Object Orientation
Python like Java™ is completely object oriented. Python has a first class object model with first class functions. Python supports multiple inheritance, classes, modules, exceptions and late (run-time) binding. 

Interpretation
Java runs on top of the Java virtual machine.  Python also runs in an interpreter. 

Memory Management
Both Java and Python support their own brand of garbage collection.

Interactivity
A Java program must be compiled into bytecode and then interpreted in a virtual machine.  A Python program can be run interactively, there is no need for the intermediary step of compiling to bytecode.  In my limited use of the Python language I found that this cuts down on my debugging time by at least 200%. 

Portability
As mentioned earlier both Java and Python are quite portable.

Performance
Being that both Java and Python are interpreted languages they cannot run as fast as native languages, but both languages support the ability to embed native code in their programs.

Extendable
Both languages support the idea of extendibility.  A module or piece of code can be dynamically loaded at run time in order to add a new feature. 

External Services
Java has a very rich API whereas Python can access many external libraries such as X11, Motif, Tk, Mac and MFC to name just a few. JPython has access to the Java APIs.

Now a look at the more technical side of Python

Modules
Both Python and Java have a mechanism for adding groupings of executable code to a program at run time by calling import. In Python, a module is a group of classes or functions that are collected in a single source file. Further grouping of modules in Python can be accomplished by placing groups of modules together in a package. Packages in Python are groups of modules collected in hierarchies of directories. In Java, every class lives in its own file. Collections of classes are known as packages. As in Python, packages in Java are often hierarchically arranged. For example, the Java statement: import java.lang.String; would allow the class String from the java.lang package to be known as String alone from within your program.

The Python import statement is defined by two steps. First, find the module and initialize it if necessary. Second, define a name for the module within the local name space (scope of where the import was called). The first time the Python interpreter imports class HelloWorld it finds, parses and compiles the Python source code file class HelloWorld.py into byte-code and stores the byte-code in class HelloWorld.pyc. Subsequent programs that import class HelloWorld, do not require a recompilation of the byte-code by the interpreter unless the source file is present and has been modified. 

Type System
While Python shares many features with Java such as being interpreted and having support for late binding, Python's type system is very different. Unlike Java, Python is a dynamically typed language. Type mismatch errors are not reported until your program is running. Fortunately, like Java, Python has a very nice exception mechanism which allows programmers to handle type and other runtime errors in an elegant and controlled fashion. There is a slight performance price to pay for such convenience.  For certain types of applications the overhead of dynamic typing can slow down programs. Where this is true, an extension module in C can be written to alleviate a bottleneck.  

Like Java, new types can be created through inheritance with the class mechanism.  In Python, all types are first-class objects. Integers, floats, strings, instances, functions, methods, classes, types and any other built in or user defined type may be passed as a parameter to a function. 

Because all entities are first-class in Python, there is no need, as there is in Java, to consider the type of a type to determine if there are any special rules to follow.  

Python may be extended with an object type written in C. These types have the advantage of executing with the speed of C and have a Python object orientation when they are manipulated. These objects are instantiated in the same way as objects that are instantiated from classes written in Python.  

Object Orientation
Python is object oriented. Unlike Java, Python supports multiple inheritance. Like Java, Python binds the names of attributes to the code that handles them at runtime. In addition, new attributes may be added to Python object classes or instances on-the-fly, simply by assigning values to them.

Classes, methods, and functions in Python are callable objects. A callable object can have arguments passed to it in a function call. Python supports optional arguments, keyword arguments, and generalized argument lists. These feature remove the need for the more verbose Method overloading used in C++ and Java because of their lack of support for variable arguments to functions. 

Access Control
Python uses a mechanism for data hiding which I find much simpler than the mechanism of Java and C++.  A name of any class attribute is made private by giving it two leading underscores (i.e. _ _myHiddenAttribute). Any class attribute may be hidden in this manner: class variables, instance variables, and methods. 

Exception Handling
Python and Java have a exception handling built into there respective run-times. Exceptions are very useful, particularly in late-binding languages such as Java and Python. Exception handling is used for controlling program flow for unexpected or error conditions. You can catch exceptions thrown by the run-time environment, by an imported module, or by your own code. In Python, exceptions are typically string objects (but may be classes) and in Java they are typically derivations of the Throwable object.

Both Java and Python have mechanism to catch groups of exceptions. Python allows the specification of an unqualified clause allowing for wildcard exception handling. Java allows any subclass of Exception to be specified in the catch clause. This allows progressions of catch clauses specify exceptions ranging from most specific to least specific. Java's exception mechanism provides a finally clause, as in Python. Finally has the side-effect of re-throwing the exception as its last statement.  

Conclusion

I hope that you found this brief introduction to Python valuable and informative.  In my opinion it is a language worth exploring.  This summary of Python centered around the 1.5 version.  Python evolves slowly so this write up should be up to date at least into mid-2000. I've read on the Usenet groups that there is a 2.0 version in the works that will improve on the already excellent object model of Python so keep your eyes open. Also, I believe JPython can be a very valuable addition to any Java programmers tool set. 

For more information on JPython check out http://www.jpython.org .  

Happy Hacking.

 
[ Home ] [ Projects ] [ Papers ] [ Open Source ] [ Infolets ] [ Senior Project ]