【问题标题】:Why I can not use this expression (int, list) in code为什么我不能在代码中使用这个表达式 (int, list)
【发布时间】:2022-07-11 22:30:27
【问题描述】:

我对 (int, list) 有疑问:

def randint_sum_equal_to(sum_value: int, n: int, lower: (int, list) = 0, upper: (int,list) = None):

"Tuple expression not allowed in type annotation Use Tuple[T1, ..., Tn] to indicate a tuple type or Union[T1, T2] to indicate a union type"

非常感谢

【问题讨论】:

  • 您是否尝试过使用Tuple[int, list] 来代替错误状态?
  • 在现代 Python 上,tuple[int, list] 也可以工作(内置类本身支持泛化,而不依赖于 typing 类)。

标签: python


【解决方案1】:

您需要导入标准类型库。

from typing import Tuple

...
def randint_sum_equal_to(sum_value: int, n: int, lower: Tuple[int, list] = 0, upper: Tuple[int, list] = None):

【讨论】:

    猜你喜欢
    • 2019-11-28
    • 2018-04-01
    • 2022-01-14
    • 1970-01-01
    • 2011-09-24
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多