Chapter 2. Jython Basics

Before we can show you how to use Jython to improve your interactions with existing Java libraries, we need to give you a tour of the Python language. The next four chapters will act as both a quick tutorial and a quick reference to using Python. In this chapter, we’ll show you how to run Jython and use the interpreter, then discuss Python’s built-in types.

Python has roughly the same number of built-in types as Java. It has fewer numeric types, but adds powerful collection objects that are much more flexible and useful than their Java counterparts. This chapter will show how to create and use those types.

Running Jython

Jython can be invoked in three ways. It can be run in an interactive interpreter session, it can be run as a batch interpreter session on a specific script file, or the jythonc compiler can be run to compile Jython code to a set of Java classes or to a single .jar file. Most of these chapters will cover the interpreter; jythonc will be covered in Chapter 13. For instructions on obtaining and installing Jython, see Appendix A.

To start an interactive session, run jython from the command line. (On Windows, you can also double-click the jython.bat file.) You will see the following:

Jython 2.0 on java1.3.0 (JIT: null)
Type "copyright", "credits" or "license" for more information.

This tells you what version of Jython you are using, and what Java Virtual Machine (JVM) is running it. The triple angle-brackets are the Jython command prompt. If you ...

Get Jython Essentials now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.