RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Java>列表

Jython:Java的Python

来源: 作者: 时间:2007-08-08 点击:
import pawt                                           # get Java awt lib
    pawt.test(Hello( ))                                        # run under awt loop

 

The Python class in this code inherits all the necessary applet protocol from the standard Java Applet superclass, so there is not much new to see here. Under Jython, Python classes can always subclass Java classes, because Python objects really are Java objects when compiled and run. The Python-coded paint method in this script will be automatically run from the Java AWT event loop as needed; it simply uses the passed-in gc user-interface handle object to draw a text message.

这段代码中的Python类从标准Java Applet父类中继承了所有必需的applet协议,所以这里并没有什么新东西。在Jython下,Python类总是能继承Java类,因为 Python对象编译完并运行时,实际上就是Java对象。这段脚本中Python编码的paint方法将会在Java AWT事件循环中自动运行,它简单地使用传入的用户界面句柄对象gc来绘制一条文本信息。

If we use Jython's jythonc command-line tool to compile this into a Java .class file and properly store that file on a web server, it can then be used exactly like applets written in Java. Because most web browsers include a JVM, this means that such Python scripts may be used as client-side programs that create sophisticated user-interface devices within the browser, and so on.

如果我们用Jython的命令行工具jythonc来将这段代码编译成Java .class文件,并放在web服务器合适的地方,它就可以像Java写的applet一样使用。因为大多数网页浏览器包含一个JVM,这就意味着这样的 Python脚本可以用作客户端程序,可以在浏览器中创建复杂的用户界面,等等。

18.4.6. Jython Trade-Offs

18.4.6. Jython的权衡

Depending on your background, the potentially less good news about Jython is that even though the calculator and applet scripts discussed here are straight Python code, the libraries they use are different from what we've seen so far. In fact, the library calls employed are radically different. The calculator, for example, relies primarily on imported Java class libraries, not on standard Python libraries. You need to understand Java's awt and swing libraries to make sense of its code, and this library skew between language implementations becomes more acute as programs grow larger. The applet example is even more Java bound: it depends both on Java user-interface libraries and on Java applet protocols.

与你的知识背景有关,不好的消息是,尽管此处讨论的计算器与applet的脚本是简单的Python代码,但它们所使用的库与我们以前所见的有所不同。实际上,所调用的库是根本不同的。例如计算器,主要是导入Java类库,而并非标准的Python库。你需要理解Java的awtswing库才能读懂这个代码,当程序增大时,这种不同语言实现之间的库差异将变得很严重。applet的例子则更是Java化的:它不仅依赖于Java用户界面库,同时依赖于 Java的applet协议。

If you are already familiar with Java libraries, this isn't an issue at all, of course. But because most of the work performed by realistic programs is done by using libraries, the fact that most Jython code relies on very different libraries makes compatibility with standard Python less potent than it may seem at first glance. To put that more strongly, apart from simple core language examples, many Jython programs won't run on the standard Python interpreter, and many standard Python programs won't work under Jython.

当然,如果你已经熟悉Java库,这就完全不成问题。但是,因为现实中程序所做的大部份工作是通过库的使用完成的,多数Jython代码依赖于很不相同的库,这一事实使得最初认为的与标准Python的兼容性变得不那么有效。更彻底点说,除了一些简单的核心语言的例子,许多Jython程序不会运行于标准的Python解释器上,而许多标准的Python程序也不会工作在Jython下。

Generally, Jython presents a number of trade-offs. I want to point out upfront that Jython is indeed an excellent Java scripting toolarguably the best one available, and most of its trade-offs are probably of little or no concern to Java developers. For instance, if you are coming to Jython from the Java world, the fact that Java libraries are at the heart of Jython scripts may be more asset than downside. But if you are presented with a choice between the standard and Java-based Python language implementations, some of Jython's implications are worth knowing about:

一般说来,Jython体现了某些权衡。首先我要指出,Jython真的是一种卓越的Java脚本工具,可能是现有最好的,其最大的权衡可能是,没有或很少关心到Java开发者。例如,如果你是Java阵营的,Jython脚本以Java库为中心这一事实,对你来说可能更多的是有利而不是不利。但是,如果你面对标准Python与基于Java的Python语言实现的选择时,你必须知道一些Jython的暗意:

(译注:应该是对Python关心太少吧?Jython好像是为Java开发者服务的。)


Jython is not fully compatible with the standard Python language

Jython与标准Python语言并非完全兼容

At this writing, Jython is not yet totally compatible with the standard Python language, as defined by the original C implementation. In subtle ways, the core Python language itself works differently in Jython. The list of incompatibilities (viewable at http://www.jython.org) will likely shrink over time but will probably never go away completely. Moreover, new language features are likely to show up later in Jython than in the standard C-based implementation.

本书所作时,Jython还不是完全兼容标准Python语言,标准是由原C语言实现定义的。核心Python语言本身与Jython有细微的不同。不兼容性列表(见http://www.jython.org)可能会随着时间缩小,但是不太可能完全消失。而且,新的语言特性可能出现在Jython,而没有在标准的基于C语言的实现中出现。

共11页: 上一页 [1] [2] [3] [4] [5] [6] [7] 8 [9] [10] [11] 下一页
[收藏] [推荐] [评论] [打印] [关闭]
上一篇:SOA Without SOAP: The Java ME Perspective
下一篇:Windows下Eclipse+CDT+MinGW+wxWidgets软件开发
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
热点关注
Google Adsense
相关文章