键入numpy.ndarray的提示/注释(PEP 484) [英] Type hinting / annotation (PEP 484) for numpy.ndarray

查看:143
本文介绍了键入numpy.ndarray的提示/注释(PEP 484)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人针对特定的 numpy.ndarray 类实施类型提示吗?

Has anyone implemented type hinting for the specific numpy.ndarray class?

现在,我正在使用 typing.Any ,但是拥有一些更具体的内容会很好.

Right now, I'm using typing.Any, but it would be nice to have something more specific.

例如,如果Numpy人为其类型别名 ="http://docs.scipy.org/doc/numpy/user/basics.creation.html#converting-python-array-like-objects-to-numpy-arrays"> array_like 对象类.更好的是,在 dtype 级别实现支持,以便支持其他对象,以及 ufunc .

For instance if the numpy people added a type alias for their array_like object class. Better yet, implement support at the dtype level, so that other objects would be supported, as well as ufunc.

推荐答案

看来typing模块是在以下位置开发的:

It looks like typing module was developed at:

https://github.com/python/typing

numpy存储库位于

https://github.com/numpy/numpy

可以在以下位置跟踪Python错误和提交

Python bugs and commits can be tracked at

http://bugs.python.org/

添加功能的通常方法是派生主存储库,开发该功能直到防弹,然后提交拉取请求.显然,在此过程的各个阶段,您都需要其他开发人员的反馈.如果您不能自己进行开发,则必须说服其他人,这是一个有价值的项目.

The usual way of adding a feature is to fork the main repository, develop the feature till it is bomb proof, and then submit a pull request. Obviously at various points in the process you want feedback from other developers. If you can't do the development yourself, then you have to convince someone else that it is a worthwhile project.

cython具有一种注释形式,可用于生成高效的C代码.

cython has a form of annotations, which it uses to generate efficient C code.

您引用了numpy文档中的array-like段落.请注意其typing信息:

You referenced the array-like paragraph in numpy documentation. Note its typing information:

一种简单的方法来确定是否可以使用array()将对象转换为numpy数组,只是简单地进行交互尝试并查看其是否有效! (Python方式).

A simple way to find out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works! (The Python Way).

换句话说,numpy开发人员拒绝固定下来.他们无法或不能用文字描述哪些对象可以或不能转换为np.ndarray.

In other words the numpy developers refuse to be pinned down. They don't, or can't, describe in words what kinds of objects can or cannot be converted to np.ndarray.

In [586]: np.array({'test':1})   # a dictionary
Out[586]: array({'test': 1}, dtype=object)

In [587]: np.array(['one','two'])  # a list
Out[587]: 
array(['one', 'two'], 
      dtype='<U3')

In [589]: np.array({'one','two'})  # a set
Out[589]: array({'one', 'two'}, dtype=object)

对于您自己的功能,类似

For your own functions, an annotation like

def foo(x: np.ndarray) -> np.ndarray:

有效.当然,如果您的函数最终调用某些通过其asanyarray传递其参数的numpy函数(就像许多人一样),则这样的注释将是不完整的,因为您的输入可能是listnp.matrix

works. Of course if your function ends up calling some numpy function that passes its argument through asanyarray (as many do), such an annotation would be incomplete, since your input could be a list, or np.matrix, etc.

在评估此问题和答案时,请注意日期. 484当时是一个相对较新的PEP,并且仍在开发中以将其用于标准Python的代码.但是看起来所提供的链接仍然有效.

When evaluating this question and answer, pay attention to the date. 484 was a relatively new PEP back then, and code to make use of it for standard Python still in development. But it looks like the links provided are still valid.

这篇关于键入numpy.ndarray的提示/注释(PEP 484)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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