Python type annotations for Enum value

柔情痞子 提交于 2019-12-09 08:23:35

问题


I have this piece of code:

import enum


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


def get_color_return_something(some_color):
    pass

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)?


回答1:


Type hinting the Color class should work:

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



回答2:


def get_color_return_something(some_color: Color):
    pass


来源:https://stackoverflow.com/questions/52624736/python-type-annotations-for-enum-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!