Jython Essentials PDF Print E-mail

by Samuele Pedroni and Noel Rappin
Published by O'Reilly

Review by Jonathan Ellis

I have several years of production Java experience, and for a day-job language it's not bad. Still, I've always had the sneaking suspicion that a language can express quicksort in three lines as clearly as this python cookbook recipe might be a more enjoyable way to spend my "fun programming hours." (Yes, I'm that kind of software geek.) At the same time, I've grown extremely comfortable with the powerful Java libraries, both in the very large Java standard class library and written by third-parties. So I picked up a copy of Jython Essentials by Samuele Pedroni, the current Jython project lead. Before covering the book itself, some introductory material is in order.

What is Jython?

Jython is an implemenation of Python for the JVM that works exceptionally closely with the Java language itself. You can, for instance, subclass Java objects in Python, or compile Python to Java bytecode.

Why target the Java Virtual Machine (JVM)?

Java is, of course, the current 800-pound gorilla of enterprise computing. EVERYONE uses Java. (Java trails closely C and C++ as the most popular languge on SourceForge.) The amount of leveragable Java source is large and growing. Not even CPAN can match the breadth of available Java libraries, and Java Web Start (which Jython supports) makes deploying Java applications absurdly simple for end users. Gone are the days of READMEs instructing the hapless downloader on the intricacies of CLASSPATH. And of course perhaps no language has had as much written about it recently as Java -- any question about the Java language or standard library is a google away. Jython allows both full interaction with Java classes on the Python side -- including subclassing Java classes -- and full control of Python from the Java side, e.g. to embed Python as a scripting language for your application.

The alternative to the JVM in the news these days is of course Microsoft's Common Language Runtime (CLR). Jim Huginin, the original author of Jython who left the project some years ago, demoed a Python for the CLR called IronPython at PyCON 2004. This is interesting because Huginin demonstrated performance that definitely doesn't suck, in marked contrast to that achieved by ActiveState earlier. Microsoft's CLR is an interesting competitor to the JVM -- and a technically superior one in some respects -- but there is a paucity of reusable code (libraries and other) for the CLR coming from anyone but Microsoft. This may change in the near future as the Mono project continues to make progress, but for now, the CLR is Not There Yet. If you want to leverage a systems language from a scripting language or vice versa, the JVM makes the most sense.

Why Jython?

When Jython was born (as JPython), it was unrivaled as a scripting language providing complete, easy two-way access with a systems language. Today there are at least two other scripting languages designed to interact with Java worth mentioning: Groovy and BeanShell. BeanShell provides an interpreter for a loosely-typed language strongly resembling Java; with the forthcoming 2.0 release it will also be able to interpret unmodified Java, which will be a unique accomplishment. Groovy is heavily influenced by Python and Ruby, but still keeps pretty close to Java-ish syntax in most respects.

What makes Jython (still) unique is that it implements a well-understood, mature language that has already earned respect on its own merit. Guido van Rossum, Python's designer, has a demonstrated track record for making decisions that foster concision, power, and elegance in code. But most important, there is no shortage of documentation on the Python language (Dive Into Python is a free book available in HTML and PDF that deserves special mention; Programming Python and Python in a Nutshell are excellent choices of the dead-tree variety). The importance of this cannot be overstated for someone writing code in a new language, and indeed, the other JVM-based scripting languages do indeed suffer -- sometimes greatly -- from a lack of documentation and sample code.

Of course, there's a negative side to this very advantage: the Python language spec (and "reference" implementation, CPython) moves independently of Jython. Some recent additions to the Python language have indeed proved difficult to support.(1) Fortunately, the jython project lead is active and respected on the python-dev list, and there is no trend of the Python designers overlooking Jython. Still, the fact remains that chasing a moving target is a thankless job, and any gaps in compatiblity have the potential of making Jython that much less attractive for those looking to leverage existing Python code.

Jython Essentials

       1. Introduction to Jython
2. Jython Basics
3. Jython Control Flow
4. Modules and Functions
5. Object-Oriented Jython
6. Using Java from Jython
7. Inheriting from Java
8. Reflection and JavaBeans
9. Using Swing
10. Using Java Libraries
11. Jython Standard Library
12. Embedding Jython Inside Java
13. Compiling Jython

Appendices

       A. Installing Jython
B. Jython Options and Registry
C. Jython Exceptions
D. Jython and CPython Differences
E. Java-to-Python Quick Reference

Jython Essentials is targetted at programmers who already know Java. Knowing Python is a plus, but only chapters 2-4 (and 11) are really Python-specific. Python is a compact language, and similar enough to Java that a competent Java programmer will quickly get up to speed. (Of course, neither Python's nor Java's standard library is particularly compact, but the languages themselves were both designed in a "practical minimalist" style.) These three chapters (67 pages) cover the basics of Python quite adequately. Note that Jython 2.1, still the current stable release of Jython as of this review, corresponds to Python 2.1; hence, the few features introduced in Python 2.2 and 2.3 are not covered by Jython Essentials. Python esoterica like metaclass hacking are also not covered (although supported by Jython, of course).

The first chapter of Jython Essentials is available online and is highly recommended reading for anyone interested in exploring what Jython has to offer. Chapter 1 gets right into demonstrating with a simple Swing example the power and convenience that Jython gives the Java standard library. Chapters 2-4, as mentioned above, present a short summary of Python. (Chapter 11 presents highlights from the Python standard library.) The remaining chapters are all Jython-specific, and deal with the various ways Jython interacts with the JVM and java libraries. Topics of particular interest are how to use and subclass Java classes from Jython (chapters 5 and 7) or vice versa (chapters 12 and 13), Python-style reflection as applicable to Jython and Java objects (chapter 9), the zxJDBC module (Chapter 10) which supplies a Pythonic wrapper to the JDBC, servlets in Jython (chapter 10), embedding the Jython interpreter in Java programs (chapter 12), and building Python applets with jythonc (chapter 13).

For Python programmers, Appendix D (Jython and CPython Differences) will also be of special interest. The list of differences is surprisingly short, and the technical reasons underpinning each are described clearly (and in more detail than in the similar document found in the Jython documentation).

What's to like?

If you're a programmer who likes the power of Java but envies the quick development or sheer elegance of Python, you owe it to yourself to look at Jython. Or, if you're a Python programmer who wants to write speed-critical sections of code in a higher level language than C, Jython is for you. In both cases Jython Essentials will serve you well.

What's not to like?

I would have liked to see even more details on the technical underpinnings of Jython, e.g. how Java's rather broken finalization model makes some code that works in CPython not work in Jython. Introductions to the Jython interpreter and jythonc architecture would have been even nicer. These topics are rather outside the scope of what is normally considered "essential" for an end-user; still, it would have been nice to have that information presented as an appendix in the only book by the current Jython project lead.

Resources

Endnotes

(1) Jythonc (which since 2.0 has generated Java code for consumption by javac rather than emitting bytecode directly) has yet to support generators due to lack of goto in java. (The Jython interpreter has no such difficulty.) Supporting machine-generated code is perhaps the sole remaining legitimate reason to implement goto in a modern language; this may not have occurred to Java's designers 10 years ago. If it did, they obviously didn't find it a compelling enough reason to justify a feature they went to some lengths to render unnecessary. Fortunately, the JVM itself does have a goto opcode and clever people have already found ways to make this into an acceptable workaround.
Jonathan Ellis is the author of the web game Carnage Blender and the maintainer of the NFC Chat Java chat server. He has contributed several patches to the Jython interpreter and standard library.

 


 
< Prev   Next >
Colocation provided by VersaServers