python 的 typing hint 可以给 ndarray 注释 shape 吗?

直接 def func() -> ndarray 让人对 shape 和 size 、dtype 这些摸不着头脑

python 的 typing hint 可以在注释 ndarray 的时候,添加 shape 和 size 、dtype 这些信息吗?

阅读 3.3k
2 个回答

不支持用其他方式吧:

import numpy as np
from numpy import ndarray

def my_function(arr: ndarray) -> ndarray:
    """
    Process the input array and return a new array.

    Parameters:
        arr (ndarray): Input array with shape (m, n) and dtype float64.

    Returns:
        ndarray: Output array with shape (n, m) and dtype float64.
    """
    return arr.T
新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题