Java - >蟒蛇? [英] Java -> Python?

查看:145
本文介绍了Java - >蟒蛇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了Python(和语法)的动态特性之外,Java没有的Python语言的一些主要特性是什么,反之亦然?

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?

推荐答案


  1. 列表推导。如果line.startswith( nee)] 非常好。

函数是一流的对象。它们可以作为参数传递给其他函数,在其他函数中定义,并具有词法范围。这样可以很容易地说出像 people.sort(key = lambda p:p.age)这样的东西,从而对一群年龄相仿的人进行排序而无需定义自定义比较器类或同样详细的东西。

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.

一切都是对象。 Java具有非对象的基本类型,这就是为什么标准库中的许多类定义了9个不同版本的函数(对于boolean,byte,char,double,float,int,long,Object,short)。 Array.sort 就是一个很好的例子。自动装箱有所帮助,虽然当事情结果为空时会让事情变得尴尬。

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.sort is a good example. Autoboxing helps, although it makes things awkward when something turns out to be null.

属性。 Python允许您创建具有只读字段,延迟生成字段的类,以及在分配时检查的字段,以确保它们永远不会为0或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.'

默认和关键字参数。在Java中,如果您想要一个最多可以包含5个可选参数的构造函数,则必须定义该构造函数的6个不同版本。并且根本没有办法说学生(姓名=Eli,年龄= 25)

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)

功能只能返回1件事。在Python中你有元组赋值,所以你可以说 spam,eggs = nee()但是在Java中你需要求助于mutable out参数或者有一个自定义类有2个字段,然后有两行额外的代码来提取这些字段。

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.

运算符重载。

通常设计更好的库。例如,要解析Java中的XML文档,你会说

文档doc = DocumentBuilderFactory.newInstance()。newDocumentBuilder()。parse(test.xml);

和Python中你说的是
doc = parse(test.xml)

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 say
doc = parse("test.xml")

无论如何,我可以继续使用更多示例,但Python总体上是一种更灵活,更具表现力的语言。它也是动态类型的,我非常喜欢,但它有一些缺点。

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.

Java比Python有更好的性能,并且有更好的工具支持。有时这些事情很重要,Java是比Python更好的语言来执行任务;尽管喜欢Python,我还是继续将Java用于一些新项目。但作为一种语言,我认为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 - >蟒蛇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆