Python DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Jython Essentials

Jython Tips for Python Programmers

by Noel Rappin, co-author of Jython Essentials
04/11/2002

In my first article about Jython, I discussed the features of the Python scripting language and its Jython implementation that would be of greatest interest to Java programmers. Jython is a complete implementation of the Python programming language written in 100 percent pure Java, and allows easy access to Java libraries.

However, if you are already a Python programmer, then you know about the features of the Python programming language. For somebody already familiar with Python, the greatest attraction of using Jython is the ability to use any of the wide range of existing Java libraries and tools while being able to still use Python as the programming language.

Jython does its best to shield a Python programmer from the details of Java, but you'll still need to know some of those details to use Java objects and libraries most effectively from Jython.

This article contains 10 tips for using Java objects in Jython. I'll try not to assume much prior knowledge of Java, but space limitations prevent me from defining all Java terms in detail. Also, in this article when I need to differentiate the standard C implementation of Python, I'll refer to it as CPython.

1. Importing Java Packages

A Java package is a collection of Java classes which are typically stored in the same directory, one class to a file. Just like Python modules, Java packages must be imported to Jython before use. In order to be imported, a Java package must be in either the Java classpath or the Python PYTHONPATH.

Java packages are imported as though they were Python modules, using the ordinary import statement. You use the fully qualified name of the module for import. To import the java.lang and java.util packages, you would type:

import java.lang
import java.util

Unlike CPython, in Jython you don't have to explicitly specify sub-packages. If you type the line:

import java

in Jython, both java.lang and java.util (and all the other sub-packages of java) are all available. This also works for standard Python modules -- in Jython, if you import os, then the os.path module is automatically imported.

Often, Jython programmers leave the full name of the Java package in the code, to emphasize the creation of Java objects. However, if you find that repetitive you can use the as clause to shorten the package name, commonly done like this:

import java.lang as lang
import java.util as util

It's recommended that you not use the from version import statement:

from java import *

in production Jython code, because of the overhead of importing all of the standard libraries, and other potential problems. In general, it's a good idea to stick to import for Java packages.

2. Creating Java Objects

Once imported, Java objects are created using the same syntax as ordinary Python objects.

x = java.lang.String()
z = java.util.HashMap()

Internally, Jython calls the appropriate Java constructor for the class given the number and type of arguments (see Tip 6 below for details) -- so the first line above calls the no-argument constructor for java.lang.String, but the line:

x = java.lang.String("Hi")

calls the one-argument constructor that expects a String. In other words, Jython is managing Java type information, so that you don't have to.

When creating Java objects (and only when creating Java objects), you can pass keyword arguments to the Jython constructor that are not part of the type signature of any Java constructor for that object. For example:

win = javax.swing.JFrame(size=(200,200))

For each keyword argument x, Jython looks for a matching public method of the class setX, and calls that method automatically. If there is no such set method, Jython raises an error.

Related Reading

Jython Essentials
By Samuele Pedroni, Noel Rappin

3. Using Objects in Python Statements

Once created, Java objects can be used in Jython statements, and will nearly always behave as though they were native Python objects (some restrictions are listed below).

Java objects can be used as the test in an if or while statement. The following Java object values are equivalent to Python false -- java.lang.Boolean FALSE, Boolean false, empty instances of java.util.Vector or java.util.Hashtable, and any empty class implementing the interfaces java.util.Dictionary, java.util.List, java.util.Map, or java.util.Collection. Note that unlike CPython, empty instances of Java strings are equivalent to Python true (empty Python strings are still false).

Any item of type java.util.Vector, java.util.Enumeration, java.util.List, or java.util.Iterator can be used as the list expression in a Jython for statement. In addition, you can use standard Python index notation (such as x[3]) for any subclass or implementer of java.util.Vector, java.util.Dictionary, java.util.List, or java.util.Map. However, slice access (such as x[3:5]) is not supported on Java objects.

Any subclass of java.lang.Exception can be raised and caught from within Jython using the normal Python try and raise statements.

Pages: 1, 2

Next Pagearrow





Sponsored by: