UFO ET IT

정의 순서대로 Python Enum 반복

ufoet 2021. 1. 8. 20:56
반응형

정의 순서대로 Python Enum 반복


python 2.7과 함께 python 3.4에서 백 포트 된 Enum 기능을 사용하고 있습니다.

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

python 3의 열거 형 문서 ( https://docs.python.org/3/library/enum.html#creating-an-enum )에 따르면 "열거 형 은 정의 순서대로 반복을 지원합니다 ". 그러나 나를 위해 반복이 일어나지 않습니다.

>>> from enum import Enum
>>> class Shake(Enum):
...     vanilla = 7
...     chocolate = 4
...     cookies = 9
...     mint = 3
...     
>>> for s in Shake:
...     print(s)
...     
Shake.mint
Shake.chocolate
Shake.vanilla
Shake.cookies

내가 뭔가를 오해하거나 정의 순서의 반복이 아직 백 포트 된 Enum 버전에서 지원되지 않습니까? 후자를 가정하면 순서대로 발생하도록 강제하는 쉬운 방법이 있습니까?


여기에서 답을 찾았습니다 : https://pypi.python.org/pypi/enum34/1.0 .

python <3.0의 경우 __order__ 속성을 지정해야합니다.

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

참조 URL : https://stackoverflow.com/questions/25982212/iterate-python-enum-in-definition-order

반응형