Chapter 5. Object-Oriented Jython

Python is an object-oriented scripting language. There is a design tension hidden in that description. Object-oriented languages generally have a number of features designed to enforce access control and encapsulation. Scripting languages, on the other hand, tend to avoid such features, reasoning that they are needlessly restrictive and hamper the programmer. If you’ve ever cursed out other programmers because they hid vital functionality without allowing access, you understand the appeal.

The Python solution to this tangle is a simple and elegant semantic model that enables object-oriented structures to harmonize with the language features already discussed with a minimum of extra syntax. Python’s simple, clear, object-oriented semantics allow you to focus on creating the right modular, object-oriented design without having to make compromises in your design to satisfy language requirements.

In this chapter, we will discuss object-oriented programming in Jython, and show how the Python scripting language helps you use Jython to support good object-oriented design principles. There are a number of things that you can do in Python that you cannot easily do in Java, and there are one or two structures that are common in Java but are difficult in Python. We’ll also see how to get your own Python classes to behave like the built-in types.

Creating Classes

Classes are created in Python using the aptly named class statement. To declare a class with no parent ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.