当您可以只使用 Java 时,为什么还要使用 Jython?

发布于 2024-07-06 16:05:29 字数 464 浏览 4 评论 0 原文

标准答案是,当您只需要编写几行代码时它很有用......

我将两种语言都集成在 Eclipse 中。 因为 Eclipse 处理编译、解释、运行等,两者“运行”完全相同。

两者的 Eclipse IDE 都很相似 - 即时“编译”、智能感知等。两者都允许使用调试视角。

如果我想测试几行 Java,我不必创建一个全新的 Java 项目 - 我只需使用 Scrapbook 功能使我能够执行 Java 表达式,而无需创建新的 Java 程序,这是快速测试现有类或评估现有类的巧妙方法。代码片段”。

Jython 允许使用 Java 库 - 但 Java 也是如此(根据定义)!

那么 Jython 还提供哪些其他优势?

The standard answer is that it's useful when you only need to write a few lines of code ...

I have both languages integrated inside of Eclipse. Because Eclipse handles the compiling, interpreting, running etc. both "run" exactly the same.

The Eclipse IDE for both is similar - instant "compilation", intellisense etc. Both allow the use of the Debug perspective.

If I want to test a few lines of Java, I don't have to create a whole new Java project - I just use the Scrapbook feature inside Eclipse which which allows me to "execute Java expressions without having to create a new Java program. This is a neat way to quickly test an existing class or evaluate a code snippet".

Jython allows the use of the Java libraries - but then so (by definition) does Java!

So what other benefits does Jython offer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

椵侞 2024-07-13 16:05:29

一个简单的示例(来自 http://coreygoldberg. blogspot.com/2008/09/python-vs-java-http-get-request.html) :

您有一个 Java 后端,并且需要执行 HTTP GET 请求。

本地:

import java.net.*;
import java.io.*;

public class JGet {
    public static void main (String[] args) throws IOException {
        try {
            URL url = new URL("http://www.google.com");

            BufferedReader in = 
                new BufferedReader(new InputStreamReader(url.openStream()));
            String str;

            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }

            in.close();
        } 
        catch (MalformedURLException e) {} 
        catch (IOException e) {}
    }
}

在 Python 中:

import urllib
print urllib.urlopen('http://www.google.com').read()

Jython 允许您使用 java 的稳健性,并在需要时使用 Python 的清晰度。

还有什么 ? 正如乔治所说...

A quick example (from http://coreygoldberg.blogspot.com/2008/09/python-vs-java-http-get-request.html) :

You have a back end in Java, and you need to perform HTTP GET resquests.

Natively :

import java.net.*;
import java.io.*;

public class JGet {
    public static void main (String[] args) throws IOException {
        try {
            URL url = new URL("http://www.google.com");

            BufferedReader in = 
                new BufferedReader(new InputStreamReader(url.openStream()));
            String str;

            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }

            in.close();
        } 
        catch (MalformedURLException e) {} 
        catch (IOException e) {}
    }
}

In Python :

import urllib
print urllib.urlopen('http://www.google.com').read()

Jython allows you to use the java robustness and when needed, the Python clarity.

What else ? As Georges would say...

奢望 2024-07-13 16:05:29

对于许多程序员来说,Python 语法(Jython 使用的)更加简洁且开发速度更快。

此外,您还可以在 Java 应用程序中使用现有的 Python 库。

Python syntax (used by Jython) is considerably more concise and quicker to develop for many programmers.

In addition, you can use existing Python libraries in a Java application.

留一抹残留的笑 2024-07-13 16:05:29

类比:当你可以喝热自来水并咀嚼烤苦豆时,为什么还要喝咖啡呢? :-)

对于某些任务,Python 的味道更好,工作得更好,而且速度足够快(需要时间来酝酿?)。 如果您的编程或部署环境专注于 JVM,Jython 允许您编写 Python 代码,但无需更改您的部署和运行时环境。

Analogy: Why drink coffee when you can instead drink hot tap water and chew on roasted bitter beans. :-)

For some tasks, Python just tastes better, works better, and is fast enough (takes time to brew?). If your programming or deployment environment is focused on the JVM, Jython lets you code Python but without changing your deployment and runtime enviroment.

魂ガ小子 2024-07-13 16:05:29

我刚刚发现了 Jython,作为一名语言学家,我想我会说这有点像问“当你可以使用法语时为什么使用拉丁语”(当然,忘记了拉丁语出现在法语之前的事实) 。

不同的人类语言实际上让你以不同的方式思考。 法语是一门很棒的语言,我在法国生活了很长时间并获得了法语学位。 但拉丁语惊人的力量和简洁性会让你的思维进入一个不同的区域,例如,可以交换词序以产生各种微妙的效果。

我认为,从我对 Jython 的粗略了解来看,它确实激发了我的热情,它会让我以不同的方式思考。 一段时间以来,我对 Python/Jython 非常怀疑,并且一直是 Java 泛型的忠实粉丝(具有讽刺意味的是,它减少了打字量,因此如果您愿意的话,可以将法语“拉丁化”)。 我不太明白像 Jython 这样的“动态类型”语言的全部含义,但我认为最好的办法就是顺其自然,看看 Jython 对我的想法有何影响!

语言的来来去去很有趣。 另一个“拉丁语”可能被认为是具有无限递归语法的 Algol68。 但是,开发大规模递归代码的需求以及阅读代码和思考代码的能力(尚未)尚未显现出来。 Jython似乎非常强大且优雅,非常适合我们现在的处境,它具有 OO 库、Java swing 的强大功能,以及所有内容都封装在一个非常优雅的包中。 也许有一天 Jython 也会采用无限递归语法?

I've just discovered Jython and, as a bit of a linguist, I think I'd say it's a bit like asking "why use Latin when you can use French" (forgetting about the fact that Latin came before French, of course).

Different human languages actually make you think in different ways. French is a great language, I've lived in France a long time and done a degree in it. But Latin's startling power and concision puts your mind into a different zone, where word order can be swapped around to produce all sorts of subtle effects, for example.

I think, from my cursory acquaintance with Jython, which has really fired up my enthusiasm by the way, that it's going to make me think in different ways. I was very sceptical about Python/Jython for some time, and have been a big fan of Java generics for example (which ironically reduce the amount of typing and therefore "latinise" the French if you like). I don't quite understand the full implications of "dynamically typed" languages like Jython, but I think the best thing is to go with the flow and see what Jython does to my mind!

It's funny how languages come and go. Another "Latin" might be considered to be Algol68 with its infinitely recursive syntax. But the need to develop massively recursive code, and the capacity to read it and think in it, has not (yet) made itself felt. Jython seems to be a very powerful and elegant fit with where we are now, with OO libraries, the power of Java swing, and everything wrapped up in a very elegant bundle. Maybe one day Jython will adopt infinitely recursive syntax too?

许你一世情深 2024-07-13 16:05:29

我使用 Jython 进行 Java 代码的交互式测试。 这通常比编写 Java 测试应用程序甚至任何脚本语言要快得多。 我可以尝试一下这些方法,看看它的反应如何。 从那里我可以学到足够的知识来编写一些真实的代码或测试用例。

I use Jython for interactive testing of Java code. This is often much faster than writing Java test applications or even any scripting language. I can just play with the methods and see how it reacts. From there I can learn enough to go and write some real code or test cases.

傾城如夢未必闌珊 2024-07-13 16:05:29

使用 Python 不仅仅是“语法糖”,除非您喜欢编写(或让您的 IDE 生成)数百行样板代码。 使用动态类型语言时,快速开发技术有其优点,但缺点是它会使 API 和集成变得复杂,因为您不再拥有同质的代码库。 这也会影响维护,因为并不是团队中的每个人都像您一样热爱 Python,并且使用它的效率也不会那么高。 这可能是个问题。

Using Python is more than "syntactic sugar" unless you enjoy writing (or having your IDE generate) hundreds of lines of boiler plate code. There's the advantage of Rapid Development techniques when using dynamically typed languages, though the disadvantage is that it complicates your API and integration because you no longer have a homogeneous codebase. This can also affect maintenance because not everybody on your team loves Python as much as you and won't be as efficient with it. That can be a problem.

甜扑 2024-07-13 16:05:29

有些任务在某些语言中比其他语言更容易。 如果我必须解析某个文件,我会立即选择 Python 而不是 Java。

Some tasks are easier in some languages then others. If I had to parse some file, I'd choose Python over Java in a blink.

顾铮苏瑾 2024-07-13 16:05:29

Python 具有函数式编程的一些特性,例如 lambda。 Java 没有这样的功能,如果有这样的支持,一些程序会更容易编写。 因此,有时用 Python 编写代码并通过 Jython 集成它比尝试用 Java 编写代码更容易。

Python has some features of functional programming, such as lambdas. Java does not have such functionality, and some programs would be considerably easier to write if such support was available. Thus it is sometimes easier to write the code in Python and integrate it via Jython that to attempt to write the code in Java.

楠木可依 2024-07-13 16:05:29

Python 库 ;) 例如 BeautifulSoup - 接受不正确标记的 HTML 解析器。 AFAIK 没有类似的纯 Java 库。

Python libraries ;) For example BeautifulSoup - an HTML parser that accepts incorrect markup. AFAIK there is no similar pure Java lib.

挽心 2024-07-13 16:05:29

无需编译。 也许您想要比使用编译语言更快地运行某些东西,例如原型。

...您可以将 Jython 解释器嵌入到您的应用程序中。 不错的功能,我不能说我用过它,但仍然很酷。

No need to compile. Maybe you want to get something rolling faster than using a compiled language, like a prototype.

...and you can embed the Jython interpreter into your apps. Nice feature, I can't say I've used it, but tis cool nonetheless.

恋竹姑娘 2024-07-13 16:05:29

Jython 还可以用作 Java 程序中的嵌入式脚本语言。 您可能会发现在某些时候使用内置扩展语言编写一些东西很有用。 如果使用 Java Jython 是一个选择(Groovy 是另一个选择)。

我主要使用 Jython 在 Java 系统上进行探索性编程。 我可以导入应用程序的某些部分,并通过从交互式 Jython 会话调用 API 来查看发生了什么。

Jython can also be used as an embedded scripting language within a Java program. You may find it useful at some point to write something with a built in extension language. If working with Java Jython is an option for this (Groovy is another).

I have mainly used Jython for exploratory programming on Java systems. I could import parts of the application and poke around the API to see what happened by invoking calls from an interactive Jython session.

温折酒 2024-07-13 16:05:29

语法糖。

Syntax sugar.

浪菊怪哟 2024-07-13 16:05:29

在你的情况下,这没有多大意义。 但这并不意味着它永远不会发生。 例如,如果您正在开发一个允许最终用户创建扩展或插件的产品,那么最好将其编写为脚本。

In your situation, it doesn't make much sense. But that doesn't mean it never does. For example, if you are developing a product that allows end users to create extensions or plugins, it might be nice for that to be scriptable.

聽兲甴掵 2024-07-13 16:05:29

将现有代码移植到新环境可能是原因之一。 您的某些业务逻辑和领域功能可能存在于 Python 中,并且编写该代码的团队坚持使用 Python。 但部署和维护它的团队可能需要 J2EE 集群的可管理性以适应规模。 您可以将 Jython 中的逻辑包装在 EAR/WAR 中,然后部署组就可以看到另一个 J2EE 捆绑包像所有其他 J2EE 捆绑包一样进行管理。

即它是处理阻抗失配的一种手段。

Porting existing code to a new environment may be one reason. Some of your business logic and domain functionality may exist in Python, and the group that writes that code insists on using Python. But the group that deploys and maintains it may want the managability of a J2EE cluster for the scale. You can wrap the logic in Jython in a EAR/WAR and then the deployment group is just seeing another J2EE bundle to be managed like all the other J2EE bundles.

i.e. it is a means to deal with an impedance mismatch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文