Does python support enumerated types?

This question already has an answer here:

  • How can I represent an 'Enum' in Python? 43 answers

  • From the Docs: Enumerations are created using the class syntax, which makes them easy to read and write. An alternative creation method is described in Functional API. To define an enumeration, subclass Enum as follows:

    >>>
    >>> from enum import Enum
    >>> class Color(Enum):
    ...     red = 1
    ...     green = 2
    ...     blue = 3
    

    Python3.4+ supports various types of enumerations in the enum module. Obviously it's not quite the same as an enumerated type in C or C++ , but it serves the same purpose. If you're stuck in an older version of python, there's a 3rd party backport available on pypi.

    Basic usage would look like:

    class Foo(enum.Enum):
        bar = 1
        foobar = 2
        blah = 3
    
    链接地址: http://www.djcxy.com/p/91754.html

    上一篇: 按键排序JavaScript对象

    下一篇: python是否支持枚举类型?