如何处理Python~静态类型? [英] How to deal with Python ~ static typing?

查看:110
本文介绍了如何处理Python~静态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java世界,我想知道在编译代码时除了缺少错误之外,Python中的动态类型有什么好处?

I am from Java world and I wonder what is so great about dynamic typing in Python besides missing errors while compiling the code?

你喜欢Python的输入吗?你有一个例子,它在一个大项目中有所帮助吗?是不是有点容易出错?

Do you like Python's typing? Do you have an example where it helped in a big project? Isn't it a bit error prone?

推荐答案

在一般情况下,静态类型检查是不可判定的。这意味着有些程序是静态类型安全的,但是类型检查程序无法证明它们是静态类型安全的,因此类型检查器必须拒绝这些程序。

Static type checking is undecidable in the general case. This means that there are programs which are statically type-safe but for which the type-checker cannot prove that they are statically type-safe, and thus the type-checker must reject those programs.

换句话说:类型检查程序不允许您编写的类型安全程序。或者,更简洁:静态类型阻止您编写某些程序。

In other words: there are type-safe programs that the type-checker will not allow you to write. Or, even more succinctly: static typing prevents you from writing certain programs.

这一般适用于所有静态类型,而不仅仅适用于Java 。

This applies to all static typing in general, not just to Java.

至于Java:它有一个相当糟糕的类型系统。它的类型系统不够表达甚至非常简单的属性。例如:在的类型中静态void java.util.Arrays.sort(Object [] a)它实际上是否说结果必须是,你知道,排序?或者说数组元素必须部分排序?

As to Java specifically: it has a rather crappy type system. Its type system is not expressive enough to express even very simple properties. For example: where in the type of static void java.util.Arrays.sort(Object[] a) does it actually say that the result has to be, you know, sorted? Or that the array elements have to be partially ordered?

Java的另一个问题是它的类型系统有很大的洞,你可以驾驶卡车:

Another problem with Java is that its type system has holes so big that you can drive a truck through:

String[] a = new String[1];
Object[] b = a;
b[0] = 1; // ArrayStoreException

这个特殊情况下的问题是协变数组。数组不可能是协变的和类型安全的。

The problem in this particular case are covariant arrays. It's simply not possible for arrays to be both covariant and type-safe.

Java结合了静态类型的所有麻烦,没有任何优点。所以,你也可以摆脱麻烦。

Java combines all the hassle of static typing with none of the advantages. So, you might just as well get rid of the hassle.

但请注意,这不是普遍的。还有其他语言有更好的类型系统,这些系统的权衡不太清楚。

However, note that this is not universal. There are other languages which have much better type systems for which the trade-offs are much less clear.

例如,这是有史以来最愚蠢的语言基准( Python中的Fibonacci):

For example, here is the most stupid language benchmark of all time (Fibonacci) in Python:

def fib(n):
    if n < 2: return n
    return fib(n-2) + fib(n-1)

和Java:

int fib(int n) {
    if (n < 2) return n;
    return fib(n-2) + fib(n-1);
}

请注意,那里有更多的混乱,这与静态打字。为了使比较更公平,让我们设想一种使用Python语法和Java语义的语言:

Note that there is quite a bit more clutter there, which is solely related to static typing. To make the comparison more fair, let's imagine a language with Python's syntax and Java's semantics:

def fib(n: int) -> int:
    if n < 2: return n
    return fib(n-2) + fib(n-1)

[有趣的注意:在Python 3.x中添加了可选的静态类型注释, 实际上也是有效的Python代码,尽管它显然仍然不是静态类型安全的,因为注释是就是这样:注释。它们实际上从未在任何地方进行过检查。]

[Interesting side note: with the addition of optional static type annotations in Python 3.x, that is actually also valid Python code, although it is obviously still not statically type-safe, since the annotations are just that: annotations. They are never actually checked anywhere.]

那里有一些明确的混乱。但是,在Haskell中它看起来像这样:

There is some definite clutter there. However, in Haskell it looks like this:

fib n
  |     n < 2 = n
  | otherwise = fib (n-2) + fib (n-1)

与Python版本不同,这个 完全静态类型安全,但没有与类型相关的混乱。

Unlike the Python version, this is perfectly statically type-safe, but there is zero type-related clutter.

在这种特殊情况下,静态的好处之间的问题而且动态类型不太清楚。

In this particular case, the question between the benefits of static and dynamic typing are much less clear.

顺便说一下,更惯用的Haskell版本可能如下所示:

By the way, a more idiomatic Haskell version would probably look like this:

fib 0 = 0
fib 1 = 1
fib n = fib (n-2) + fib (n-1)

或者这个:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

真的,更重要的是Java和Python之间的区别并不在于Java是静态类型的,而是动态类型化的Python,而是Java不是一种优秀的编程语言,而Python 。所以,Java总是会失败,不是因为它是静态类型的,而是因为它是垃圾。比较BASIC和Haskell,Haskell明显获胜,但同样,不是因为它是静态类型的,而是因为BASIC是垃圾。

Really, the much more important difference between Java and Python is not so much that Java is statically typed and Python is dynamically typed, but rather that Java is just not a good programming language, while Python is. So, Java is just always going to lose, not because it is statically typed, but because it is crap. Comparing BASIC with Haskell, Haskell clearly wins, but again, not because it is statically typed but because BASIC is crap.

更有趣的比较是Java vs. BASIC或Python与Haskell。

A much more interesting comparison would be Java vs. BASIC or Python vs. Haskell.

这篇关于如何处理Python~静态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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