枚举属性的类型注释 [英] Type annotations for Enum attribute

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

问题描述

我有这段代码:

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 variable in this function, if I suppose that I'll receive an enum attribute 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)

这篇关于枚举属性的类型注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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