【发布时间】:2017-01-16 19:28:32
【问题描述】:
我有一个函数只能返回a、b 或c,它们都是T 类型。我想在签名中包含这个事实,因为它们在函数上下文中具有特殊含义。我该怎么做?
目前,我使用这个
def fun(...) -> "a or b or c":
#briefly explain the meaning of a, b and c in its docstring
那个是正确的吗?
我知道我能做到
def fun(...) -> T:
# briefly explain the meaning of a, b and c in its docstring
但正如我所说,我想在签名中表示该函数只返回那些特定的值。
【问题讨论】:
-
为什么不创建这些特定值的枚举,然后指定返回的类型是那个枚举?
标签: python python-3.x type-hinting literals python-typing