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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/96922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:18:58  来源:igfitidea点击:

Why use Jython when you could just use Java?

javaeclipsejython

提问by nzpcmad

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.

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

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

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

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 Scrapbookfeature 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".

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

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

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

So what other benefits does Jython offer?

那么 Jython 还提供哪些其他好处?

回答by e-satis

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

一个简单的例子(来自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.

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

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 :

在 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.

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

What else ? As Georges would say...

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

回答by Ben Hoffstein

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

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

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

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

回答by Aaron

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.

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

回答by mike rodent

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).

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

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!

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

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 seemsto 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?

语言来来去去很有趣。另一个“拉丁语”可能被认为是具有无限递归语法的 Algol68。但是开发大规模递归代码的需求,以及阅读和思考代码的能力,还没有(还)让人感觉到。Jython似乎与我们现在所处的环境非常强大和优雅,有 OO 库、Java Swing 的强大功能以及所有东西都包含在一个非常优雅的包中。也许有一天 Jython 也会采用无限递归语法?

回答by John Meagher

I use Jython for interactivetesting 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.

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

回答by Marcio Aguiar

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.

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

回答by Loren Segal

Using Python is more than "syntactic sugar" unless you enjoywriting (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.

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

回答by sumek

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

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

回答by toluju

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.

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

回答by ConcernedOfTunbridgeWells

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).

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

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.

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