为Enum值键入注释 [英] Type annotations for Enum value

查看:198
本文介绍了为Enum值键入注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

import enum


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


def get_color_return_something(some_color):
    pass

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

How do I properly add type annotations to the some_color varaible in this function, if I suppose to receive a value from the Color enum (for example: Color.RED)?

推荐答案

提示Color类应该工作的类型:

Type hinting the Color class should work:

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

这篇关于为Enum值键入注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆