简体   繁体   English

使用类型提示将Python转换为Cython

[英]Using type hints to translate Python to Cython

Type Hints now are available in Python 3.5 version. 类型提示现在可以在Python 3.5版本中使用。 In the specification ( PEP 484 ) the goals (and the non-goals) are exposed clearly: 在规范( PEP 484 )中,目标(和非目标)被清楚地暴露出来:

Rationale and Goals 理由和目标

This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information. 该PEP旨在为类型注释提供标准语法,打开Python代码以更容易地进行静态分析和重构,潜在的运行时类型检查,以及(可能在某些上下文中)利用类型信息生成代码。 [...] [...]

Of these goals, static analysis is the most important. 在这些目标中,静态分析是最重要的。

Non-goals 非目标

Using type hints for performance optimizations is left as an exercise for the reader. 使用类型提示进行性能优化仍然是读者的练习。

On the other hand, Cython has been using for a long time static syntax to improve performance. 另一方面,Cython已经使用了很长时间的静态语法来提高性能。 Usually, people rewrite some pieces of their code with Cython syntax, compile them, and then import them back as independent modules. 通常,人们用Cython语法重写一些代码,编译它们,然后将它们作为独立模块导回。 It's a painful job do all that on a large code base. 在大型代码库上完成所有这些工作是一项痛苦的工作。 But the worst part is that even when you follow correctly all the above steps, you don't have any guarantee that you'll have a real improvement (because of compatibility problems that might be caused if you are using some modules). 但最糟糕的是,即使您正确地遵循了上述所有步骤,您也无法保证您将获得真正的改进(因为如果您使用某些模块可能会导致兼容性问题)。

Would be a difficult task write a tool that uses this new type hints things scattered in the code to automatically translate them to Cython syntax and possibly do the rest of the job (compile them into modules and import all them back)? 编写一个使用这种新类型提示的工具是一项艰巨的任务, 提示代码中散布的东西会自动将它们转换为Cython语法,并可能完成剩余的工作(将它们编译成模块并将它们全部导回)? It would be possible, therefore, to share the same language syntax in all the code base. 因此,可以在所有代码库中共享相同的语言语法。

Theoretically, it's possible to write a tool like that, but I'm not sure if be worth (in terms of complexity to write it and the real improvement that would be yield). 从理论上讲,有可能编写一个这样的工具,但我不确定是否值得(就编写它的复杂性和可能产生的真正改进而言)。

Thanks. 谢谢。

Someone else just asked about 484 and Cython, PEP-484 Type Annotations with own types , and I responded with a thread from a couple of months back about 484 and numpy. 其他人只是询问了484和Cython, PEP-484类型注释以及自己的类型 ,我在几个月后回复了一个关于484和numpy的帖子。

I have doubts about the suitability of this topic for Stackoverflow. 我怀疑这个主题对Stackoverflow的适用性。 It's a research topic,not a 'how do I solve this coding problem' question. 这是一个研究课题,而不是“我如何解决这个编码问题”的问题。

Based on limited reading, the type-hints in 484 are preliminary, and any use is limited to the code checker developed by the 484 authors. 基于有限的阅读,484中的类型提示是初步的,并且任何使用仅限于由484位作者开发的代码检查器。 Py3 has had annotations for a long time, but I've seen very few examples of code that includes them. Py3已经有很长一段时间的注释,但我看到很少有包含它们的代码示例。 Certainly not in the numpy code that I focus on here. 当然不是我在这里关注的numpy代码。

Another point is that cython and numpy (and numba ) are used with Py2 just as much, if not more, than Py3. 另一点是cythonnumpy (和numba )与Py2一起使用,如果不是更多,则与Py3一样多。 So the latest bells-n-whistles in Py 3.5 are generally ignored by these users. 因此,这些用户通常会忽略Py 3.5中最新的铃声。 The @ operator is the only recent addition that numpy users value. @运算符是numpy用户重视的唯一新增功能。

You are welcome respond, but I may nominate this question for closure based on it being a duplicate or off topic. 欢迎您回复,但我可以提出这个问题,因为它是重复或非主题的。

The typing module is being developed at https://github.com/python/typing typing模块正在https://github.com/python/typing上开发

mypy is the type checker based on 484, https://github.com/python/mypy (funny, ~/mypy is the directory where I put all my SO testing scripts.) mypy是基于484的类型检查器, https://github.com/python/mypy (搞笑, ~/mypy是我放置所有SO测试脚本的目录。)

That's where cutting edge Python type checking work is being done, not here. 这就是前沿Python类型检查工作正在进行的地方,而不是这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM