Iterate python Enum in definition order

后端 未结 1 591
無奈伤痛
無奈伤痛 2021-01-31 13:46

I\'m using the backported Enum functionality from python 3.4 with python 2.7:

> python --version
Python 2.7.6
> pip install enum34
# Installs version 1.0..         


        
相关标签:
1条回答
  • 2021-01-31 14:17

    I found the answer here: https://pypi.python.org/pypi/enum34/1.0.

    For python <3.0, you need to specify an __order__ attribute:

    >>> from enum import Enum
    >>> class Shake(Enum):
    ...     __order__ = 'vanilla chocolate cookies mint'
    ...     vanilla = 7
    ...     chocolate = 4
    ...     cookies = 9
    ...     mint = 3
    ...     
    >>> for s in Shake:
    ...     print(s)
    ...     
    Shake.vanilla
    Shake.chocolate
    Shake.cookies
    Shake.mint
    
    0 讨论(0)
提交回复
热议问题