Java - > Python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49824/
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
Java -> Python?
提问by jodonnell
Besides the dynamic nature of Python (and the syntax), what are some of the major features of the Python language that Java doesn't have, and vice versa?
除了 Python 的动态特性(和语法)之外,Java 没有的 Python 语言的一些主要特性是什么,反之亦然?
回答by Eli Courtwright
List comprehensions. I often find myself filtering/mapping lists, and being able to say
[line.replace("spam","eggs") for line in open("somefile.txt") if line.startswith("nee")]is really nice.Functions are first class objects. They can be passed as parameters to other functions, defined inside other function, and have lexical scope. This makes it really easy to say things like
people.sort(key=lambda p: p.age)and thus sort a bunch of people on their age without having to define a custom comparator class or something equally verbose.Everything is an object. Java has basic types which aren't objects, which is why many classes in the standard library define 9 different versions of functions (for boolean, byte, char, double, float, int, long, Object, short).
Array.sortis a good example. Autoboxing helps, although it makes things awkward when something turns out to be null.Properties. Python lets you create classes with read-only fields, lazily-generated fields, as well as fields which are checked upon assignment to make sure they're never 0 or null or whatever you want to guard against, etc.'
Default and keyword arguments. In Java if you want a constructor that can take up to 5 optional arguments, you must define 6 different versions of that constructor. And there's no way at all to say
Student(name="Eli", age=25)Functions can only return 1 thing. In Python you have tuple assignment, so you can say
spam, eggs = nee()but in Java you'd need to either resort to mutable out parameters or have a custom class with 2 fields and then have two additional lines of code to extract those fields.Built-in syntax for lists and dictionaries.
Operator Overloading.
Generally better designed libraries. For example, to parse an XML document in Java, you say
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("test.xml");
and in Python you saydoc = parse("test.xml")
列出理解。我经常发现自己过滤/映射列表,并且能够说
[line.replace("spam","eggs") for line in open("somefile.txt") if line.startswith("nee")]真的很好。函数是第一类对象。它们可以作为参数传递给其他函数,在其他函数中定义,并具有词法作用域。这使得很容易说出这样的话
people.sort(key=lambda p: p.age),从而对一群人按他们的年龄进行排序,而无需定义自定义比较器类或同样冗长的东西。一切都是对象。Java 具有不是对象的基本类型,这就是为什么标准库中的许多类定义了 9 种不同版本的函数(对于 boolean、byte、char、double、float、int、long、Object、short)。
Array.sort是一个很好的例子。自动装箱有帮助,尽管当结果为空时它会让事情变得尴尬。特性。Python 允许您创建具有只读字段、延迟生成字段以及在分配时检查的字段以确保它们永远不会为 0 或 null 或任何您想要防范的字段等的类。
默认和关键字参数。在 Java 中,如果您想要一个最多可以接受 5 个可选参数的构造函数,则必须定义该构造函数的 6 个不同版本。而且完全没有办法说
Student(name="Eli", age=25)函数只能返回 1 个东西。在 Python 中,您有元组分配,因此您可以说,
spam, eggs = nee()但在 Java 中,您需要使用可变输出参数或拥有一个包含 2 个字段的自定义类,然后有两行额外的代码来提取这些字段。列表和字典的内置语法。
运算符重载。
通常设计更好的库。例如,要在 Java 中解析 XML 文档,您说,
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("test.xml");
而在 Python 中,您说doc = parse("test.xml")
Anyway, I could go on and on with further examples, but Python is just overall a much more flexible and expressive language. It's also dynamically typed, which I really like, but which comes with some disadvantages.
无论如何,我可以继续举出更多的例子,但 Python 总体上只是一种更灵活和更具表现力的语言。它也是动态类型的,我真的很喜欢它,但它有一些缺点。
Java has much better performance than Python and has way better tool support. Sometimes those things matter a lot and Java is the better language than Python for a task; I continue to use Java for some new projects despite liking Python a lot more. But as a language I think Python is superior for most things I find myself needing to accomplish.
Java 的性能比 Python 好得多,并且有更好的工具支持。有时这些事情很重要,Java 是比 Python 更好的语言。尽管我更喜欢 Python,但我继续在一些新项目中使用 Java。但作为一门语言,我认为 Python 在我发现自己需要完成的大多数事情上都更胜一筹。
回答by dF.
I think this pair of articles by Philip J. Eby does a great job discussing the differences between the two languages (mostly about philosophy/mentality rather than specific language features).
我认为 Philip J. Eby 的这对文章在讨论两种语言之间的差异(主要是关于哲学/心态而不是特定的语言特征)方面做得很好。
回答by Dave Webb
One key difference in Python is significant whitespace. This puts a lot of people off - me too for a long time - but once you get going it seems natural and makes much more sense than ;s everywhere.
Python 中的一个主要区别是显着的空格。这让很多人望而却步 - 我也是很长一段时间 - 但是一旦你开始,它看起来很自然并且比;任何地方都更有意义。
From a personal perspective, Python has the following benefits over Java:
从个人角度来看,Python 比 Java 有以下优点:
- No Checked Exceptions
- Optional Arguments
- Much less boilerplate and less verbose generally
- 无检查异常
- 可选参数
- 更少的样板文件和更少的冗长
Other than those, this page on the Python Wikiis a good place to look with lots of links to interesting articles.
除此之外,Python Wiki 上的这个页面是一个查看有趣文章链接的好地方。
回答by Bob Nadler
回答by Mario F
Apart from what Eli Courtwright said:
除了 Eli Courtwright 所说的:
- I find iterators in Python more concise. You can use for i in something, and it works with pretty much everything. Yeah, Java has gotten better since 1.5, but for example you can iterate through a string in python with this same construct.
- Introspection: In python you can get at runtime information about an object or a module about its symbols, methods, or even its docstrings. You can also instantiate them dynamically. Java has some of this, but usually in Java it takes half a page of code to get an instance of a class, whereas in Python it is about 3 lines. And as far as I know the docstrings thing is not available in Java
- 我发现 Python 中的迭代器更简洁。你可以在 something 中使用for i,它几乎适用于所有东西。是的,Java 从 1.5 开始变得更好,但是例如,您可以使用相同的构造在 python 中迭代一个字符串。
- 自省:在 Python 中,您可以在运行时获取有关对象或模块的有关符号、方法甚至文档字符串的信息。您还可以动态实例化它们。Java 有一些这样的,但在 Java 中通常需要半页代码来获取类的实例,而在 Python 中它大约需要 3 行。据我所知,文档字符串在 Java 中不可用

