What's So Good About Jython? - What is Python? (Page 2 of 4 )
Jython is an implementation of Python that is written in pure Java. This means that Jython offers all that any other implementation of Python offers (see next section), but also provides access to the whole range of Java library classes (such as Swing, JDBC, Java Cryptography, Java Speech API, and so on). It also makes it very easy to write Python code that integrates existing Java components. Conversely, it is easy to write Jython components that can later be reused and integrated into other Java-based systems. The benefit to the Java developer is rapid application development without sacrificing functionality, robustness, or the commercial respect afforded by Java.
Python is a general-purpose, object-oriented, scripting language. It is a highly regarded language that is gaining in popularity because it offers high productivity and therefore competitive advantage. It also has a simple syntax that gives rise to readable (and maintainable) programs.
The language itself contains most of the constructs and features that you might expect, such as objects, functions (methods), procedural loop constructs, and exception handling. The main feature for a C or Java programmer is, arguably, the ease with which one can create and manipulate lists and sequences of values. This, coupled with idioms of functional programming such as mapping and filtering, make for a very powerful core language.
A Jython Example
As I stated earlier, Jython is an implementation of Python that is written in pure Java. This is such a powerful idea that I'm surprised how little Jython has been recognised and adopted. Let me illustrate the marriage of the two languages with a little example. Afterwards, we can compare the equivalent source codes for Java and Jython. (If you would like to try out the example for yourself and you do not already have an installation of Jython, you can download it from the website at www.jython.org.)
I will explain the example in terms of a session with the interactive shell, so that you understand not only how the source code works, but also how you might work with the Jython interpreter. So let’s start up Jython's interactive shell by typing ‘jython’ at the command line. You should see something like the following:
C:\My Jython>jython Jython 2.0 on java1.4.0 (JIT: null) Type "copyright", "credits" or "license" for more information. >>>
Now type the following at the >>> prompt:
import javax.swing as swing
Jython accepts the import, and simply displays the next prompt, waiting for another line of input:
C:\My Jython>jython Jython 2.0 on java1.4.0 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> import javax.swing as swing >>>
The import statement allows us to use the shorter package name 'swing' as the name of the javax.swing package. We can now create an instance of a JFrame, give it a title, make it visible, and assign it to a variable, f, all in one line:
f=swing.JFrame(title="My Frame", visible=1)
There are several features of Jython that allow this line to be so short. Firstly, we don't need to declare variables before using them. (This can be a mixed blessing, as Jython is more willing to accept typos.) Secondly, we don't use the Java keyword ‘new’ for creating an instance of a class. Thirdly, properties can be set on a JavaBean at the time of its creation by passing them as keyword arguments. Here, we are setting two properties: the name of the JFrame, and its visibility. Note that Python does not have a Boolean type, as in Java, so we must supply the visibility value as 0 (false) or 1 (true).
At this point, the JFrame is visible, so you can read its title, but it has no size. To make the window bigger, you resize it by setting the size property of the object f:
There is quite a lot going on in this short line. Firstly, the dot notation ('f.size'), combined with assignment, is a short hand for setting the value of a JavaBean property. We could have called the setSize() method directly on f (as in f.setSize(300,300)), but using the dot notation for setting property values can lead to more concise code. Note that we did not explicitly create an instance of the java.awt.Dimension class before assigning it to the size property. Jython knows to expect a java.awt.Dimension object, and therefore passes the tuple (300, 300) as an argument to the constructor of the Dimension class to create a new Dimension object. The newly created Dimension object is then set as the value of the size property. Our frame still doesn't do anything, so let's first create, and then add, a button:
b=swing.JButton("Push Me") f.contentPane.add(b)
You'll need to redraw the frame before the button becomes visible on-screen. You can do this manually by resizing the frame with the mouse, or programmatically by calling
Now let's make the button print a message whenever it is pressed. First, we define a function that prints a message:
def printMessage(event): print 'Ouch!'
Next, we associate that function with the button b.
And that's it! If you press the button, the console says 'Ouch!'. We illustrate the differences between Java and Jython code for this example by listing the source code for each. First, the Java source code:
You can see how much shorter the Jython code is, even for such a simple example. Think of the implications for the cost of code development and maintenance!
If you have tried out this example for yourself, or already used Jython, you will have noticed another great feature of the language — the immediate feedback you get from the interpreter. Many programmers criticize interpreted languages for executing slowly, but when it comes to speed of code development the interpreter wins hands-down over a compiler. The interactive nature of an interpreter means that you detect some errors much earlier than you would with an edit-compile-test loop. And if you can both interpret and compile code (as with Jython) then you get the best of both approaches.