首页 > 解决方案 > 元组的 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

标签: pythontyping

解决方案


正确的语法是

from typing import Tuple

### ---- Examples ----- ###
Tuple[float, float]
Tuple[float, str]


推荐阅读