Jython (formerly known as JPython) is an entirely distinct implementation of the Python programming language that allows programmers to use Python as an easy-to-use scripting component in Java-based applications. The effect is much the same as deploying standard Python as a scripting language for C and C++ libraries.
Jython(以前叫JPython)是Python编程语言一个完全独特的实现,它让程序员能够在基于Java的应用中使用Python,使Python成为一个易用的脚本部件。其效果如同部署标准Python作为C和C++库的脚本语言。
In short, Jython makes Python code look like Java and consequently offers a variety of technology options inherited from the Java world. With Jython, Python code may be run as client-side applets in web browsers, as server-side scripts, and in a variety of other roles. Jython is distinct from other systems mentioned in this section in terms of its scope: while it is based on the core Python language we've seen in this book, it actually replaces the underlying implementation of that language rather than augmenting it.[*]
简单地说,Jython使Python代码看起来像Java代码,因此可以使用各种源自Java世界的技术。利用Jython,Python代码可以作为客户端applet运行于网页浏览器,也可以用作服务器端的脚本,以及用于其它多种场合。Jython在范畴上不同于本章所述的其它系统:它虽然基于核心的Python言语,但它实际上是替换了Python语言的底层实现而不是扩展它。[*]
[*] Jython was the second complete implementation of the Python language. By contrast, the standard, original implementation of Python is sometimes now referred to as CPython, because it is implemented in ANSI C. Among other things, the Jython implementation is driving a clearer definition of the Python language itself, independent of a particular implementation's effects. The newer IronPython Python implementation for Microsoft's C#/.NET environment (mentioned later in this chapter in the sidebar "The IronPython C# Python Compiler") is a third Python implementation, and like Jython, it is also helping to form a definition of what it means to be Python.
[*] Jython是Python语言的第二个完整实现。原来标准的Python现在有时被称为CPython,因为它是ANSI C的实现。除了别的以外,Jython实现自身具有对Python语言的更清晰的定义,不受其它特定实现的影响。微软的C#/.NET环境下新的 Python实现IronPython(将在本章后面的边栏“IronPython C# Python编译器”中提及),是第三个Python实现,与Jython一样,也是形成了自己对Python的定义。
This section briefly explores Jython and highlights some of the reasons you may or may not want to use it instead of the standard Python implementation. Although Jython is primarily of interest to programmers writing Java-based applications, it underscores integration possibilities and language definition issues that merit the attention of Python users. Because Jython is Java-centric, you need to know something about Java development to make the most sense of Jython, and this book doesn't pretend to teach that in the next few pages. For more details, interested readers should consult other materials, including Jython documentation at http://www.jython.org.
本节将概要地浏览Jython,并突出用Jython替换标准Python的一些原因,以及不应该替换的原因。尽管Jython主要用于编写基于Java 的应用,但它强调了集成的可能性和语言定义问题,值得Python用户关注。因为Jython是以Java为中心的,你需要了解一些Java才能搞清楚 Jython,而老实说,本书下面几页并不能教会你Java和Jython。有兴趣的读者应该阅读其它资料以获取更多内容,其中包括Jython文档 http://www.jython.org。
|
18.4.1. A Quick Introduction to Jython
18.4.1. Jython简介
Functionally speaking, Jython is a collection of Java classes that run Python code. It consists of a Python compiler, written in Java, that translates Python scripts to Java bytecode so they can be executed by a Java Virtual Machinethe runtime component that executes Java programs and is used by major web browsers. Moreover, Jython automatically exposes all Java class libraries for use in Python scripts. In a nutshell, here's what comes with the Jython system:
从功能上说,Jython是一些可以运行Python代码的Java类。它包含一个Python编译器,是用Java写的,它能将Python脚本翻译成 Java字节码,使之能运行于Java虚拟机,即大部份浏览器所使用的能运行Java程序的虚拟机。另外,Jython自动导出了所有Java类库,使之能用于Python脚本。概括起来,以下就是Jython系统所提供的:
Python-to-Java-bytecode compiler
Python到Java字节码的编译器
-
Jython always compiles Python source code into Java bytecode and passes it to a Java Virtual Machine (JVM) runtime engine to be executed. A command-line compiler program, jythonc, is also able to translate Python source code files into Java .class and .jar files, which can then be used as Java applets, beans, servlets, and so on. To the JVM, Python code run through Jython looks the same as Java code. Besides making Python code work on a JVM, Jython code also inherits all aspects of the Java runtime system, including Java's garbage collection and security models.

