This book will continue to evolve and we will continually update both the online version and the printed copy. We would like to advocate that the community continues to stay involved with the book. If you would like to post comments or suggestions for the book or if you find errors, please submit them via apress.
Thanks for reading this book, and for developing with the Jython language. We had a great time working on this book and hope that you enjoy reading it just as much. We look forward to continually updating this book, and seeing what the future will hold for Jython. Surely if Jython remains as active as it is today, we will all enjoy it long into the future. Language and Syntax Elegant is an adjective that is often used to describe the Python language. It is a fact that Python is an elegant language that lets one create powerful applications in an uncomplicated manner.
The ability to make reading and writing complex software easier is the objective of all programming languages, and Python does just that. There are also C and. NET versions of Python with multiplatform support. So, Python can run nearly everywhere. In this book, we focus on Jython, the language implementation that takes the elegance, power, and ease of Python and runs it on the JVM.
The Java platform is an asset to the Jython language much like the C libraries are for Python. Jython is able to run just about everywhere, which gives lots of flexibility when deciding how to implement an application. Not only does the Java platform allow for flexibility with regards to application deployment, but it also offers a vast library containing thousands of APIs that are available for use by Jython.
Add in the maturity of the Java platform and it becomes easy to see why Jython is such an attractive programming language. The goal, if you will, of any programming language is to grant its developers the same experience that Jython does. Simply put, learning Jython will be an asset to any developer. Once you have experienced the power of programming on the Java platform, it will be difficult to move away from it. Learning Jython not only allows you to run on the JVM, but it also allows you to learn a new way to harness the power of the platform.
The language increases productivity as it has an easily understood syntax that reads almost as if it were pseudocode. It also adds dynamic abilities that are not available in the Java language itself. In this chapter you will learn how to install and configure your environment, and you will also get an overview of those features that the Python language has to offer.
This chapter is not intended to delve so deep into the concepts of syntax as to bore you, but rather to give you a quick and informative introduction to the syntax so that you will know the basics and learn the language as you move on through the book. It will also allow you the chance to compare some Java examples with those which are written in Python so you can see some of the advantages this language has to offer.
By the time you have completed this chapter, you should know the basic structure and organization that Python code should follow. It will give you a taste of using statements and expressions. As you move through the book, you will use this chapter as a reference to the basics. This chapter will not cover each feature in completion, but it will give you enough basic knowledge to start using the Python language.
Throughout this book, you will be learning how to use the Python language, and along the way we will show you where the Jython implementation differs from CPython, which is the canonical implementation of Python written in the C language. It is important to note that the Python language syntax remains consistent throughout the different implementations. At the time of this writing, there are three mainstream implementations of Python.
NET platform. At the time of this writing, CPython is the most prevalent of the implementations. Therefore if you see the word Python somewhere, it could well be referring to that implementation. This book will reference the Python language in sections regarding the language syntax or functionality that is inherent to the language itself.
The Definitive Guide to Jython
However, the book will reference the name Jython when discussing functionality and techniques that are specific to the Java platform implementation. Along the way, you will learn how to program in Python and advanced techniques. Developers from all languages and backgrounds will benefit from this book. Whether you are interested in learning Python for the first time or discovering Jython techniques and advanced concepts, this book is a good fit. Java developers and those who are new to the Python language will find specific interest in reading through Part I of this book as it will teach the Python language from the basics to more advanced concepts.
Often in this reference, you will see Java code compared with Python code. To get started, you will need to obtain a copy of Jython from the official website www. Because this book focuses on release 2. You will see that there are previous releases that are available to you, but they do not contain many of the features which have been included in the 2. Jython implementation maintains consistent features which match those in the Python language for each version. For example, if you download the Jython 2.
Similarly, when using the 2. There are also some extra pieces included with the 2. Please grab a copy of the most recent version of the Jython 2. You will see that the release is packaged as a cross-platform executable JAR file. Right away, you can see the obvious advantage of running on the Java platform. In order to install the Jython language, you will need to have Java 5 or greater installed on your machine.
You can initiate the Jython installer by simply double-clicking on the JAR file. It will run you through a series of standard installation questions. In order to run Jython, you will need to invoke the jython. This is where our journey begins! The Jython interactive interpreter is a great place to evaluate code and learn the language. It is a real-time testing environment that allows you to type code and instantly see the result. As you are reading through this chapter, I recommend you open up the Jython interpreter and follow along with the code examples.
Identifiers and Declaring Variables Every programming language needs to contain the ability to capture or calculate values and store them. Python is no exception, and doing so is quite easy. Defining variables in Python is very similar to other languages such as Java, but there are a few differences that you need to note. To define a variable in the Python language, you simply name it using an identifier. An identifier is a name that is used to identify an object.
- The Definitive Guide To Jython | bygyzohigeta.cf.
- Inside the Third Reich.
- The Big Four (Hercule Poirot, Book 5).
- The Definitive Guide to Jython (Engels).
- Popular covers.
- Account Options.
The language treats the variable name as a label that points to a value. It does not give any type for the value. Therefore, this allows any variable to hold any type of data. It also allows the ability of having one variable contain of different data types throughout the life cycle of a program. So a variable that is originally assigned with an integer, can later contain a String. Identifiers in Python can consist of any ordering of letters, numbers, or underscores. However, an identifier must always begin with a non-numeric character value.
We can use identifiers to name any type of variable, block, or object in Python. As with most other programming languages, once an identifier is defined, it can be referenced elsewhere in the program. Once declared, a variable is untyped and can take any value. This is one difference between using a statically typed language such as Java, and using dynamic languages like Python.
In Java, you need to declare the type of variable which you are creating, and you do not in Python. It may not sound like very much at first, but this ability can lead to some extraordinary results. Listing We simply choose a name and assign it a value. Since we do not need to declare a type for the variable, we can change it to a different value and type later in the program. This is a key to the dynamic language philosophy.
The Definitive Guide to Jython: Python for the Java™ Platform
Let us take what we know so far and apply it to some simple calculations. Based upon the definition of a variable in Python, we can assign an integer value to a variable, and change it to a float at a later point. For instance: Listing Not here, Python allows us to bypass type constriction and gives us an easy way to do it. Reserved Words There are a few more rules to creating identifiers that we must follow in order to adhere to the Python language standard. Certain words are not to be used as identifiers as the Python language reserves them for performing a specific role within our programs.
These words which cannot be used are known as reserved words. If we try to use one of these reserved words as an identifier, we will see a SyntaxError thrown as Python wants these reserved words as its own. There are no symbols allowed in identifiers. Table lists all of the Python language reserved words: Table Reserved Words and. It is important to take care when naming variables so that you do not choose a name that matches one of the module names from the standard library.
Coding Structure Another key factor in which Python differs from other languages is its coding structure. Back in the day, we had to develop programs based upon a very strict structure such that certain pieces must begin and. Python uses indentation rather than punctuation to define the structure of code. Unlike languages such as Java that use brackets to open or close a code block, Python uses spacing as to make code easier to read and also limit unnecessary symbols in your code.
It strictly enforces ordered and organized code but it lets the programmer define the rules for indentation, although a standard of four characters exists. Although you may not yet be familiar with this construct, I think you will agree that it is easy to determine the outcome. This technique must be adhered to throughout an entire Python application. By doing so, we gain a couple of major benefits: easy-to-read code and no need to use curly braces.
There is no need to do so when using Python as the spacing takes care of this for you. It is also worth noting that the Java code in the example could have been written on one line, or worse, but we chose to format it nicely. Python ensures that each block of code adheres to its defined spacing strategy in a consistent manner.
What is the defined spacing strategy? You decide. As long as the first line of a code block is outdented by at least one space, the rest of the block can maintain a consistent indentation, which makes code easy to read. Many argue that it is the structuring technique that Python adheres to which makes them so easy to read. No doubt, adhering to a standard spacing throughout an application makes for organization. As mentioned previously, the Python standard spacing technique is to use four characters for indentation. If you adhere to these standards then your code will be easy to read and maintain in the future.
Your brain seems hard-wired to adhering to some form of indentation, so Python and your brain are wired up the same way. Operators The operators that are used by Python are very similar to those used in other languages As you can see from the following examples, there is no special trick to using any of these operators. Perhaps the most important thing to note with calculations is that if you are performing calculations based on integer values then you will receive a rounded result.
If you are performing calculations based upon floats then you will receive float results, and so on. A good rule of thumb is that if your application requires precise calculations to be defined, then it is best to use float values for all of your numeric variables, or else you will run into a rounding issue.
In Python 2. This operator was introduced as a segue way for changing integer division in future releases so that the result would be a true division. Expressions Expressions are just what they sound like. They are a piece of Python code that can be evaluated and produces a value. Expressions are not instructions to the interpreter, but rather a combination of values. If we wish to perform a calculation based upon two variables or numeric values then we are producing an expression.
The examples of expressions that are shown above are very simplistic. Expressions can be made to be very complex and perform powerful computations. They can be combined together to produce complex results. Functions Oftentimes it is nice to take suites of code that perform specific tasks and extract them into their own unit of functionality so that the code can be reused in numerous places without retyping each time.
A common way to define a reusable piece of code is to create a function. Functions are named portions of code that perform that usually perform one or more tasks and return a value. In order to define a function we use the def statement. The def statement is used to define a function. Here is a simple piece of pseudocode that shows how to use it. As you can see, def precedes the function name and parameter list when defining a function.
As you can see, the function contains one line of code which is a print statement. We will discuss the print statement in more detail later in this chapter; however, all you need to know now is that it is used to print some text to the screen. In this case, we print a simple message whenever the function is called. Functions can accept parameters, or other program variables, that can be used within the context of the function to perform some task and return a value. Specifically, we assign 25 to x and 7 to y in the example.
The function then takes x and y, performs a calculation and returns the result.
Download The Definitive Guide To Jython Python For The Java Platform
Functions in Python are just like other variables and they be passed around as parameters to other functions if needed. Here we show a basic example of passing one function to another function. For more detail on using def and functions, please take a look at Chapter 4, which is all about functions.
Classes Python is an object-oriented programming language. Much like building blocks are used for constructing buildings, each object in Python can be put together to build pieces of programs or entire programs. This section will give you a brief introduction to Python classes, which are one of the keys to object orientation in this language.
Classes are defined using the class keyword. Classes can contain functions, methods, and variables. Methods are just like functions in that the def keyword is used to create them, and they accept parameters. The only difference is that methods take a parameter known as self that refers to the object to which the method belongs. Classes contain what is known as an initializer method, and it is called automatically when a class is instantiated.
The class accepts two parameters, x and y. An initializer also defines what values can be passed to a class in order to create an object. You can see that each method and function within the class accepts the self argument. The self argument is used to refer to the object itself, this is how the class shares variables and such.
The self keyword is similar to this in Java code. The x and y variables in the example are named self. While working with code within the object, you can refer to these variables as self. If you create the object and assign a name to it such as obj1, then you can refer to these same variables as obj1. As you can see, the class is called by passing the values 7 and 8 to it. These values are then assigned to x and y within the class initializer method.
We assign the class object to an identifier that we call obj1. The obj1 identifier can now be used to call methods and functions that are defined within the class. For more information on classes, please see Chapter 6, which covers object orientation in Python. Classes are very powerful and the fundamental building blocks for making larger programs. Statements When we refer to statements, we are really referring to a line of code that contains an instruction that does something. A statement tells the Python interpreter to perform a task.
Ultimately, programs are made up of a combination of expressions and statements. In this section, we will take a tour of statement keywords and learn how they can be used. I will not cover every statement keyword in this section as some of them are better left for later in the chapter or the book, but you should have a good idea of how to code an action which performs a task after reading through this section.
Download The Definitive Guide To Jython Python For The Java Platform
While this section will provide implementation details about the different statements, you should refer to later chapters to find advanced uses of these features. It is important to remember that you cannot use any of these keywords for variable names. If the expression evaluates to True then one set of statements will be executed, and if it evaluates to False a different set of statements will be executed.
If statements are quite often used for branching code into one direction or another based upon certain values which have been calculated or provided in the code. Pseudocode would be as follows: Listing When there are multiple expressions to be evaluated in the same statement, then the elif statement can be used to link these expressions together. Note that each set of statements within an if-elif-else statement must be indented with the conditional statement out-dented and the resulting set of statements indented. Remember, a consistent indentation must be followed throughout the course of the program.
The if statement is a good example of how well the consistent use of indention helps readability of a program. This can lead to code that is very hard to read…the indentation which Python requires really shines through here. It can be used for displaying messages, which are printed from within a program, and also for printing values, which may have been calculated. In order to display variable values within a print statement, we need to learn how to use some of the formatting options that are available to Python.
This section will cover the basics of using the print statement along with how to display values by formatting your strings of text. In the Java language, we need to make a call to the System library in order to print something to the command line. In Python, this can be done with the use of the print statement. The most basic use of the print statement is to display a line of text. In order to do so, you simply enclose the text that you want to display within single or double quotes.
Take a look at the following example written in Java, and compare it to the example immediately following which is rewritten in Python. Java Print Output Example System. Python Print Output Example print 'This text will be printed to the command line' As you can see from this example, printing a line of text in Python is very straightforward. We can also print variable values to the screen using the print statement. Once again, very straightforward in terms of printing values of variables. Simply place the variable within a print statement.
We can also use this technique in order to append the values of variables to a line of text. If you try to append a numeric value to a String you will end up with an error. So in order to perform this task correctly we will need to use some of the aforementioned Python formatting options. This is easy and powerful to do, and it allows one to place any content or value into a print statement. Each of the formatting operators, which are included in the string of text, will be replaced with the corresponding values from those variables at the end of the print statement.
The next example shows that we also have the option of using expressions as opposed to variables within our statement. The formatting operator that is used determines how the output looks, it does not matter what type of input is passed to the operator. Take a look at the following example to see the output for each of the different formatting operators. If we simply need to find out the value of a variable during processing then it is easy to display using the print statement.
Using this technique can often really assist in debugging and writing your code. The idea is that we try to run a piece of code and if it fails then it is caught and the error is handled in a proper fashion. We all know that if someone is using a program that displays an ugly long error message, it is not usually appreciated. Using the try-except-finally statement to properly catch and handle our errors can mitigate an ugly program dump. This approach is the same concept that is used within many languages, including Java.
There are a number of defined error types within the Python programming language and we can leverage these error types in order to facilitate the try-except-finally process. When one of the defined error types is caught, then a suite of code can be coded for handling the error, or can simply be logged, ignored, and so on. The main idea is to avoid those ugly error messages and handle them neatly by displaying a formatted error message or performing another process.
Houston, we have a problem. If there is an exception that is caught within the block of code and we need a way to perform some cleanup tasks, we would place the cleanup code within the finally clause of the block. All code within the finally clause is always invoked before the exception is raised. The details of this topic can be read about more in Chapter 7. You can place a raise statement anywhere that you wish to raise a specified exception.
There are a number of defined exceptions within the language which can be raised. For instance, NameError is raised when a specific piece of code is undefined or has no name. For a complete list of exceptions in Python, please visit Chapter 7. In order to save a program so that it can be used later, we place the code into files on our computer. Files that contain Python code should contain a. These files are known as modules in the Python world. Explore Now. Buy As Gift. Overview Jython is an open source implementation of the high-level, dynamic, object-oriented scripting language Python seamlessly integrated with the Java platform.
About the Author Josh Juneau has been developing software since the mids. Josh has worked with Java in the form of graphical user interface, web, and command-line programming for several years. His interest in learning new languages that run on the JVM led to his interest in Jython. Since , Josh has been the editor and publisher for the Jython Monthly newsletter. In late , he began a podcast dedicated to the Jython programming language. Jim Baker has more than 15 years of professional software development experience, focusing on business intelligence, enterprise system management, and high-performance web applications.
Jim graduated from Harvard College magna cum laude with a bachelor's degree in computer science, and earned his master's in science from Brown University, where he was an all-but-dissertation Ph. Frank Wierzbicki is head of the Jython project and a lead software developer at Sauce Labs. He has been programming since the Commodore 64, was the king of home computers look it up kids! Frank's most enduring hobby is picking up new programming languages, but he has yet to find one that is more fun to work with than Python.
Leonardo Soto has been part of the Jython development team since the middle of , after a successfully completed Google Summer of Code project that aimed to run and integrate the Django web framework with Jython. He attended the Universidad de Santiago de Chile for informatics engineering. He has developed several software systems over the past seven years, most of them being web applications, and most of them based on the JavaEE formerly J2EE platform.
However, he has been spoiled by Python since almost the start of his professional developer career, and he has missed its power and clarity countless times, which inexorably turned him toward the Jython project.
Victor Ng has been slinging Python code in enterprises for about 10 years and has worked in the banking, adventure travel, and telecommunications industries. He started a small consulting company with his friend in called Monkeybean, Inc. He lives just outside of Toronto, Ontario. She is also contributing editor for the online magazine, iWindoWatch. Throughout her career, she has been involved in software, teaching, and knowledge management in different forms, from being a technical writer for a software company to working as a college curriculum developer and instructor.
The Definitive Guide to Jython: Python for the Java Platform Jython is an open source implementation of the high-level, dynamic, object-oriented scripting language Python seamlessly integrated with the Java platform. Jython is freely available for both commercial and noncommercial use and is distributed with source code.
Jython is complementary to Java. This book begins with a brief introduction to the language and then journeys through Jython's different features and uses. The Definitive Guide to Jython i Josh Juneau has been developing software since the mid s. Josh has worked with Java in the form of GUI, web, and command-line programming for several years. His interest in learning new languages that run on the JVM led to his interest in Jython. Since , Josh has been the editor and publisher for the Jython Monthly newsletter. In late , he began a podcast dedicated to the Jython programming language.
Convert currency. Add to Basket. KG, Germany, Condition: New. Language: English. Brand new Book. Jython is an open source implementation of the high-level, dynamic, object-oriented scripting language Python seamlessly integrated with the Java platform. The Definitive Guide to Jython is organized for beginners as well as advanced users of the language.