Introduction to Jython
Botanists know that if you cross-pollinate two different strains of plants, you create a hybrid; a new plant that exhibits the strengths of both its parents. Jython is a programming hybrid. It is an implementation of the Python scripting language written in 100% pure Java that runs under any compliant Java Virtual Machine (JVM). Using Jython, you can write Python programs that interact with any Java code. The Jython interpreter supports a number of shortcuts, which make using Java libraries as easy as using your own Python code.
Jython, then, combines a scripting language of unparalleled ease of development with an operating environment for which many powerful tools have been written. The combination can be immensely valuable for programmers, enabling you to cut development time significantly while still retaining the ability to use existing Java tools, such as applets and servlets. Just as Python was originally conceived as glue to bind together other programs, Jython acts as glue to help you get the most out of Java-based tools. In particular, Jython excels at simplifying the use of complex Java programming libraries and APIs, such as the Swing graphical interface toolkit or the JDBC database connectivity API.
This chapter is your road map to the rest of the book. We'll start with an extended example of using Jython to interactively develop a small graphical interface. Then we'll discuss why Jython will make it easier for you to get things done. Finally, we'll walk through the rest of the chapters.
To run the examples in this chapter and in the rest of the book, you need to have both Jython and a JVM installed. You can get Jython at http://www.jython.org/; see Appendix A for full details. Unless otherwise specified, all Jython examples will run under Jython Version 2.0 or higher, and a fully compliant JVM running Java Version 1.2 or higher. For the examples in this book, we used Sun Microsystem's Java Development Kit, Version 1.3, but other JVMs should work as well.
Before we offer arguments for why you should be using Jython, let's first show you what Jython looks like. For your first look at Jython we're going to do a five-finger exercise from the Jython interactive prompt, which will show off many of Jython's features. The goal here is to give you a sense of how the integration of Jython and Java libraries works, and how easy it is to play with running code using Jython. In the interest of ease of display, we're going to be a little looser with good Jython style than we'll be in the longer examples later in the book--this is just a demonstration, but we hope you'll find it fun and productive. You may find it useful to type along with this exercise to really get the feel.
Start by running Jython. You should see an interactive prompt.
Jython 2.1a3 on java1.3.0 (JIT: null)
Type "copyright", "credits" or "license" for more information.
From this prompt you can type any Jython code and see the result. We're going to type in some Swing GUI code, so the first thing we need to do is import Swing:
>>> import javax.swing as swing
Whether you are a Java or Python programmer already, that line
probably looks a little odd to you. We have told Jython to import the Java
package javax.swing and refer to it using the
swing. Unlike a Java import, we don't
need to tell Jython which classes to import, and unlike an ordinary Python
import, this is importing Java code that was not specifically written to
interact with Jython.
TIP: Throughout this book, we'll use the word "Python" for things that are common to both Jython and the original implementation of Python. Using the name "Jython" is usually a signal that the concept is applicable only to Jython. Information relevant only to the C-language implementation is referred to using the word "CPython." For example, "Python uses indentation to determine block boundaries, Jython lets you subclass Java classes, and CPython compiles to .pyc files."
Next, we can create a window object:
>>> win = swing.JFrame("Welcome to Jython")
Nothing has happened visibly, but we can confirm that the object has been created by typing the identifier name at the prompt.
BorderLayout,resizable,title=Welcome to Jython,defaultCloseOperation=HIDE_ON_
(The output probably won't be bold on your machine, we're just
doing that here for emphasis.) What we've done here is use the Python syntax
for instance creation to trigger a call to a Java class constructor. Also
notice what we did not have to do--namely, declare
win as a variable or specify a type for it in any way.
The second line uses the Python interpreter to trigger the Java
toString( ) method on the
Now, we didn't set the size of the window. The first line of code will do the actual set, and the second will get the value and print it to confirm.
>>> win.size = (200, 200)
If you are a Java programmer, that code probably looks a little
too simple to be true, because
size is a private
field of the window. In fact, Jython is using introspection to infer that
win.size on the left side of the assignment refers
size property, and automatically triggers a
Java call to
win.setSize( ). On the right side of
the assignment, Jython is performing a similar inference. Taking the sequence
200), and knowing
size is of type
Dimension, Jython converts the sequence to a Java
Dimension object (by calling the appropriate
Dimension constructor) before making the assignment.
win.size standing alone in the
second line triggers a call to the Java method
win.getSize( ). So, Jython allows you to write code that
retains Python's simplicity and type flexibility, but still allows the objects
to behave like Java objects.
Let's get that window on the screen.
>>> win.show( )
Nothing happens on the command prompt, but a blank window with a
"Welcome to Jython" title, and size 200 by 200 should show up on your monitor.
If you accidentally close it, just type
again. Being able to interact with concrete objects can be of immense help in
Adding Items to the Window
The window seems rather dull, so let's do something for it.
>>> field = swing.JTextField(preferredSize=(200,20))
javax.swing.JTextField[,0,0,200x20,. . .
>>> win.pack( )
We cut off the output for clarity. At the end of this sequence,
the window should have a text field in it (and probably resize itself as
well). This time, we put the preferred size setting in the call to the
constructor. As you may know,
JTextFields in Java
don't take preferred size as an argument to the constructor. Again, Jython
infers the correct property call when the object is constructed.
Type something in the text box. Then go back to the Jython prompt to type:
Of course, you'll get whatever you typed into the box, not what we typed. This shows off the fact that Jython is interacting with the Java objects in both directions--again, excellent for debugging.
Now, let's put some buttons in.
>>> names = ["Groucho", "Chico", "Harpo"]