|
|
|
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 Interpretation Memory
Management Interactivity Portability Performance Extendable External
Services Now a look at the more technical side of Python Modules 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 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 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 Exception
Handling 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 ] |