使用类型提示将Python转换为Cython [英] Using type hints to translate Python to Cython

查看:77
本文介绍了使用类型提示将Python转换为Cython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型提示现在在Python 3.5版本中可用.在规范( PEP 484 )中,目标(以及非目标)暴露清楚:

Type Hints now are available in Python 3.5 version. In the specification (PEP 484) the goals (and the non-goals) are exposed clearly:

理论与目标

此PEP旨在为类型注释提供标准语法,开放Python代码以简化静态分析和重构,潜在的运行时类型检查,以及(可能在某些情况下)利用类型信息生成代码.[...]

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. [...]

在这些目标中,静态分析是最重要的.

Of these goals, static analysis is the most important.

非目标

使用类型提示进行性能优化留给读者练习.

另一方面,Cython长期以来一直使用静态语法来提高性能.通常,人们使用Cython语法重写部分代码,进行编译,然后将其作为独立模块导入.在大型代码库上完成所有这些工作真是一件痛苦的事情.但是最糟糕的是,即使您正确地执行了上述所有步骤,也无法保证会得到真正的改善(因为使用某些模块可能会引起兼容性问题).

On the other hand, Cython has been using for a long time static syntax to improve performance. Usually, people rewrite some pieces of their code with Cython syntax, compile them, and then import them back as independent modules. 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).

编写一个工具,使用此新类型提示,将代码中分散的内容自动转换为Cython语法,并可能完成其余的工作,将是一项艰巨的任务工作(将它们编译成模块,然后全部导入回去)?因此,有可能在所有代码库中共享相同的语言语法.

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)? 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).

谢谢.

推荐答案

其他人刚刚问了关于484和Cython的问题,

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.

我对此主题是否适用于Stackoverflow感到怀疑.这是一个研究主题,而不是如何解决此编码问题"的问题.

I have doubts about the suitability of this topic for Stackoverflow. It's a research topic,not a 'how do I solve this coding problem' question.

基于有限的阅读,484中的类型提示只是初步的,其使用仅限于484作者开发的代码检查器.Py3拥有注解已有很长时间了,但是我很少看到包含注解的示例代码.当然不在我重点关注的 numpy 代码中.

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. Py3 has had annotations for a long time, but I've seen very few examples of code that includes them. Certainly not in the numpy code that I focus on here.

另一点是,Py2与 cython 和 numpy (以及 numba )一起使用的次数与Py3一样多,甚至更多.因此,这些用户通常会忽略Py 3.5中最新的bells-n-whistles. @ 运算符是 numpy 用户重视的唯一最近添加的内容.

Another point is that cython and numpy (and numba) are used with Py2 just as much, if not more, than Py3. So the latest bells-n-whistles in Py 3.5 are generally ignored by these users. The @ operator is the only recent addition that numpy users value.

欢迎您回答,但是我可以提名这个问题作为关闭或重复的话题.

You are welcome respond, but I may nominate this question for closure based on it being a duplicate or off topic.

正在 https://github.com/python上开发 typing 模块/typing

The typing module is being developed at https://github.com/python/typing

mypy 是基于484的类型检查器, https://github.com/python/mypy (有趣的是,〜/mypy 是我放置所有SO测试脚本的目录.)

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.)

这是尖端的Python类型检查工作正在完成的地方,而不是这里.

That's where cutting edge Python type checking work is being done, not here.

这篇关于使用类型提示将Python转换为Cython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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