我应该强制 Python 类型检查吗? [英] Should I force Python type checking?

查看:25
本文介绍了我应该强制 Python 类型检查吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许作为我使用强类型语言 (Java) 的残余,我经常发现自己编写函数然后强制类型检查.例如:

Perhaps as a remnant of my days with a strongly-typed language (Java), I often find myself writing functions and then forcing type checks. For example:

def orSearch(d, query):
    assert (type(d) == dict)
    assert (type(query) == list)

我应该继续这样做吗?这样做/不这样做有什么好处?

Should I keep doing this? what are the advantages to doing/not doing this?

推荐答案

停止这样做.

使用动态"语言(对于值* 是强类型的,对于变量是无类型的,以及后期绑定)的要点是您的函数可以适当地多态,因为它们将处理任何支持您的函数所依赖的接口(鸭子输入").

The point of using a "dynamic" language (that is strongly typed as to values*, untyped as to variables, and late bound) is that your functions can be properly polymorphic, in that they will cope with any object which supports the interface your function relies on ("duck typing").

Python 定义了许多通用协议(例如 iterable),不同类型的对象可以实现这些协议而不相互关联.协议不是本身一种语言特性(与 java 接口不同).

Python defines a number of common protocols (e.g. iterable) which different types of object may implement without being related to each other. Protocols are not per se a language feature (unlike a java interface).

这样做的实际结果是,一般来说,只要你理解你语言中的类型,并且适当地注释(包括使用文档字符串,所以其他人也理解你程序中的类型),你通常可以少写一些代码,因为您不必围绕您的类型系统进行编码.您最终不会为不同的类型编写相同的代码,只是使用不同的类型声明(即使类处于不相交的层次结构中),并且您不必弄清楚哪些强制转换是安全的,哪些不安全,如果您想尝试只写一段代码.

The practical upshot of this is that in general, as long as you understand the types in your language, and you comment appropriately (including with docstrings, so other people also understand the types in your programme), you can generally write less code, because you don't have to code around your type system. You won't end up writing the same code for different types, just with different type declarations (even if the classes are in disjoint hierarchies), and you won't have to figure out which casts are safe and which are not, if you want to try to write just the one piece of code.

理论上还有其他语言提供相同的功能:类型推断语言.最流行的是 C++(使用模板)和 Haskell.理论上(并且可能在实践中),您最终可以编写更少的代码,因为类型是静态解析的,因此您不必编写异常处理程序来处理传递错误的类型.我发现他们仍然要求你对类型系统进行编程,而不是对你程序中的实际类型进行编程(他们的类型系统是定理证明者,为了便于处理,他们不会分析你的整个程序).如果这听起来不错,请考虑使用其中一种语言而不是 python(或 ruby​​、smalltalk 或 lisp 的任何变体).

There are other languages that theoretically offer the same thing: type inferred languages. The most popular are C++ (using templates) and Haskell. In theory (and probably in practice), you can end up writing even less code, because types are resolved statically, so you won't have to write exception handlers to deal with being passed the wrong type. I find that they still require you to programme to the type system, rather than to the actual types in your programme (their type systems are theorem provers, and to be tractable, they don't analyse your whole programme). If that sounds great to you, consider using one of those languages instead of python (or ruby, smalltalk, or any variant of lisp).

在 Python(或任何类似的动态语言)中,您需要使用异常来捕获对象不支持特定方法的情况,而不是类型测试.在这种情况下,要么让它进入堆栈,要么捕获它,并引发关于不正确类型的异常.这种请求宽恕而不是许可"的编码是惯用的python,并且大大有助于简化代码.

Instead of type testing, in python (or any similar dynamic language) you'll want to use exceptions to catch when an object does not support a particular method. In that case, either let it go up the stack, or catch it, and raise your exception about an improper type. This type of "better to ask forgiveness than permission" coding is idiomatic python, and greatly contributes to simpler code.

* 在实践中.在 Python 和 Smalltalk 中可以更改类,但很少见.这也与使用低级语言进行转换不同.

* In practice. Class changes are possible in Python and Smalltalk, but rare. It's also not the same as casting in a low level language.

更新:您可以使用 mypy 在生产之外静态检查您的 Python.注释您的代码以便他们可以检查他们的代码是否一致,如果他们愿意,可以让他们这样做;或者如果他们愿意的话.

Update: You can use mypy to statically check your python outside of production. Annotating your code so they can check that their code is consistent lets them do that if they want; or yolo it if they want.

这篇关于我应该强制 Python 类型检查吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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