python - 如何定义类型提示以告诉函数期望python中的类型
问题描述
为了记录我的项目/保持代码整洁,我想为我的所有函数提供一个类型提示(我对整个打字概念还是很陌生)。
我想告诉一个函数期待一个数据类型,比如 str、bool、int、dict 等。也就是你通过调用得到的type(a_var)
我意识到存在属性设置器,但是对于我的用例来说编写一个设置器是不可行的
class TypeValidator:
def __init__(self,
type: **the crux of the problem**
):
self.type = type
def validate(self, value):
return isinstance(value, self.value)
编辑:重读了我自己的问题,我想我想问的是如何在打字时描述这个:
>>> type(type)
<class 'type'>
>>>
edit2:TypeVar是我正在寻找的,感谢Patrick Haugh
解决方案
推荐阅读
- android - FCM 在 JSON 格式中创建错误
- cuda - 无论如何打印dim3值 - 网格尺寸,Cuda中的块尺寸?
- java - 如何从 JSONObject 获取下一个值
- opencv - 使用 optimize_for_inference 和 graph_transform 工具后,Inception V3 重新训练模型错误预测
- mongodb - MongoDB 和 Mongoose 正确模式
- python - 并行迭代文档行
- c# - Azure CosmosDB Mongo C# 驱动程序 ReplaceOneAsync 抛出“请求大小太大”
- python - 不支持缓存 sha2 密码 mysql
- javascript - 骨干路由器功能 goto 不存在
- function - 八度函数如何隔离值并打印它们