python 参数类型提示是否支持嵌套类型信息? [英] Do python parameter type hints support nested type information?

查看:33
本文介绍了python 参数类型提示是否支持嵌套类型信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个逻辑上的函数如下:

I have a function that is logically as follows:

 def computeProbability(
     x_i: np.array(np.int32), 
     colProbabilities: list(dict(string,np.float32))
 ) -> list(double):
     return []  # placeholder

我的猜测是这里可以实现的最严格的类型检查是:

My guess is that the most stringent type checking that can be achieved here would be:

def computeProbability(x_i: np.array, colProbabilities: list) -> list:
     return []  # placeholder

这个假设正确吗?

推荐答案

实际上 可以提供 deep 类型信息.刚刚从 python 3.5+ 中发现了 type hints.这些非常适合 IDE:例如你可以这样做

Actually it is possible to give deep type information. Just discovered type hints from python 3.5+. These are great for IDE's: e.g. you can do this

ListOfDict = List[Dict[str, float]]

然后声明一个方法,该方法返回一个带有字符串键和浮点值的 Dicts 列表

then declare a method that returns a list of Dicts with string key and float value

from typing import List, Dict
Vector = List[float]
ListOfDict = List[Dict[str, float]]

使用:

def computeLikelihood(x_i_vals: Vector, allProbs: ListOfDict):

现在我们可以在 IDE 中获取方法提示了!

Now we can get method hints in the IDE!

这是开发嵌套数据结构的一大胜利!

This is a big win for developing nested data structures!

这篇关于python 参数类型提示是否支持嵌套类型信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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