Jython Essentials and over one million other books are available for Amazon Kindle . Learn more


or
Sign in to turn on 1-Click ordering.
or
Amazon Prime free trial required. Sign up when you check out. Learn more
More Buying Choices
Have one to sell? Sell yours here
or
Get a £3.75 Amazon.co.uk Gift Card
Jython Essentials
 
 
Start reading Jython Essentials on your Kindle in under a minute.

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

Jython Essentials [Paperback]

Samuele Pedroni , Noel Rappin
5.0 out of 5 stars  See all reviews (1 customer review)
RRP: £26.99
Price: £22.94 & this item Delivered FREE in the UK with Super Saver Delivery. See details and conditions
You Save: £4.05 (15%)
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
In stock.
Dispatched from and sold by Amazon.co.uk. Gift-wrap available.
Only 2 left in stock--order soon (more on the way).
Want guaranteed delivery by Thursday, May 31? Choose Express delivery at checkout. See Details
‹  Return to Product Overview

Product Description

James Richards, GameJUG, August 2002

In examining this valuable scripting extension to the Java programming model, this fills a much needed gap in the information surrounding this language.

Product Description

Jython is an implementation of the Python programming language written in 100% pure Java, so it runs under any compliant Java Virtual Machine. The secret to Jython's popularity lies in the combination of Java's libraries and tools with Python's rapid development capabilities. With Jython, you can write Python programs that integrate seamlessly with any Java code. And like Python, Jython can be used interactively, so you can get immediate results as you are programming.

Jython Essentials provides a solid introduction to the Python language, offering a brief but thorough tour of the Python concepts you'll need to understand to use Jython effectively. The book makes frequent comparisons between Python and Java, with special emphasis on the different object-oriented semantics of the two languages, so Java programmers can quickly get up to speed with Jython.

Jython Essentials also covers the various ways in which Jython and Java can interact. For example, Jython code can create instances of pre-existing Java classes and call methods in those instances. You can write Jython classes that are direct subclasses of existing Java classes and use introspection to discern the capabilities of JavaBeans components. This book provides examples of using Jython with existing Java libraries, including the Swing GUI toolkit, the JDBC database API, the Servlet API, and various XML tools. And finally, the book shows how Jython can be used as a scripting language within a Java program.

With Jython Essentials, you have everything you need to start creating applications that mix the best of Python's interactivity and Java's robust libraries.

About the Author

Samuele Pedroni is one of the main Jython developers. He holds a CS flavored diploma in mathematics from the ETH Zurich (Swiss institute of technology in Zurich). He is now a teacher and research assistant at the Institute of Theoretical CS. He plans to come to the US for a PhD in the field of dynamic languages and dynamic compilation. He came to Jython with the interest in improving it with respect to Java importing and reloading. He has developed several important patches related to java integration, classloaders, and the reworking of java/python importing rules and design.

Noel Rappin has a Ph.D. in computer science from the Georgia Institute of Technology, where his research included methods for teaching Object-Oriented Programming and Design. He has extensive production experience in both Java and Python. Noel also contributed an introductory chapter to the book Squeak: Open Personal Computing and Multimedia (PH).

Excerpted from Jython Essentials by Samele Pedroni, Noel Rappin. Copyright © 2002. Reprinted by permission. All rights reserved.

Chapter 1 - 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.

Jython Requirements
To run the examples in this chapter and in the rest of the book, you need to have both Jython and a JVM installed.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.

Welcome to Jython
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.

Starting Jython
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 identifier 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.

>>> win
javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,resizable,title=Welcome to Jython,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=null,alignmentY=null,border=,flags=1538,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]

(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 win object.

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)
>>> win.size
java.awt.Dimension[width=200,height=200]

‹  Return to Product Overview

Amazon.co.uk Privacy Statement Amazon.co.uk Delivery Information Amazon.co.uk Returns & Exchanges