枚举值的类型注释

问题描述 投票:24回答:2

我有这段代码:

import enum


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


def get_color_return_something(some_color):
    pass

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

python python-3.x type-hinting typing type-annotation
2个回答
27
投票

提示Color类的类型应该起作用:

def get_color_return_something(some_color: Color):
    print(some_color.value)

2
投票
def get_color_return_something(some_color: Color):
    pass
© www.soinside.com 2019 - 2024. All rights reserved.