python - 元组的 Python 类型
问题描述
我只是想在 python 3.85 中定义一个元组的类型。但是,文档中的两种方法似乎都不能正常工作:
Tuple(float,str)
结果:
Traceback (most recent call last):
File "<ipython-input-30-7964c1934b1f>", line 1, in <module>
Tuple(float,str)
File "C:\Users\kinsm\anaconda3\lib\typing.py", line 727, in __call__
raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type Tuple cannot be instantiated; use tuple() instead
相对:
tuple(float,str)
结果:
Traceback (most recent call last):
File "<ipython-input-29-fea16b9491a0>", line 1, in <module>
tuple(float,str)
result:
TypeError: tuple expected at most 1 argument, got 2
解决方案
正确的语法是
from typing import Tuple
### ---- Examples ----- ###
Tuple[float, float]
Tuple[float, str]
推荐阅读
- python - HTML表单返回显示python代码而不是执行
- node.js - 带有 Cloudflare API 的请求模块返回“无效的内容类型”
- python - BeautifulSoup + For loop Zip - 如何尝试/排除其中一个
- sql - 插入具有相同 ID 的 Select
- python - 了解列表理解中的python嵌套迭代
- permissions - Manjaro 中的 Firebird - 尝试打开文件时出错权限被拒绝
- javascript - 使用 AJAX 和 Flask 更改图像
- javascript - Twilio 接收传真,正文为空
- excel - 根据另一个单元格的值记录日期值
- unity3d - 使用 AddExplosionForce 时无法将类型“float”隐式转换为“UnityEngine.Vector3”