Python 为元组输入

wil*_*man 9 python typing

我只是想在 python 3.85 中定义元组的类型。然而,文档中的两种方法似乎都无法正常工作:

Tuple(float,str)
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

相对:

tuple(float,str)
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

Kev*_* Lu 12

正确的语法是

from typing import Tuple

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

Run Code Online (Sandbox Code Playgroud)