python - Python:如何注释变量,它可以包含任何类型的通用别名,如 Union、List 等?
问题描述
那么,如何注释变量或函数 arg,它可以包含任何类型的通用别名,如 Union、List 等,即可以用于某些合理类型注释的东西,例如在 pydantic 模型中?
from typing import Union, Tuple, List
def f(x: 'What is there?'): ...
f(Union[int, str]) # should be ok
f(Tuple[int, str]) # should be ok
f(List[int]) # should be ok
f(1) # should be NOT ok
解决方案
推荐阅读
- php - 使用 ajax 嵌套资源控制器和路由
- excel - 显示打印对话框
- next.js - 每次重新加载 nextjs 材料 ui 样式丢失甚至添加材料 ui nextjs github 存储库的 _app.js 和 _document.js 但重新加载样式后仍然丢失
- node.js - 为下一个连接设置路由页面
- r - 将模型列表(randomForest)应用于 data.frames 列表以获取因素列表
- maven-3 - Maven项目重复依赖警告
- javascript - 将所有 onclick 更改为事件侦听器
- airflow - 气流 - 远大的期望 - 获取/设置配置变量
- ssl - 在应用启动时从 SSL 认证验证中禁用 Kestrel
- javascript - 单击主页以外的其他选项卡时Material-ui选项卡指示器消失