相关疑难解决方法(0)

枚举值的Python类型注释

我有这段代码:

import enum


class Color(enum.Enum):
    RED = '1'
    BLUE = '2'
    GREEN = '3'


def get_color_return_something(some_color):
    pass
Run Code Online (Sandbox Code Playgroud)

some_color如果我想从Color枚举中接收值(例如:),如何在此函数中的变量中正确添加类型注释Color.RED

python typing type-hinting python-3.x type-annotation

20
推荐指数
3
解决办法
5283
查看次数

什么是正确的类型提示提示只返回一组特定值的函数?

我有一个只能返回的函数a,b或者c它们都属于类型,T但是我想将它的一部分签名这个事实,因为它们在函数的上下文中具有特殊含义,我是如何做到的?

目前我用这个

def fun(...) -> "a or b or c":
    #briefly explain the meaning of a, b and c in its docstring
Run Code Online (Sandbox Code Playgroud)

那是正确的吗?

我知道我可以做到这一点

def fun(...) -> T:
    #briefly explain the meaning of a, b and c in its docstring
Run Code Online (Sandbox Code Playgroud)

但正如我所说,我想在签名中表达该函数只返回那些特定的值

python literals type-hinting python-3.x

8
推荐指数
2
解决办法
2396
查看次数