반응형
정의 순서대로 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
반응형
'UFO ET IT' 카테고리의 다른 글
파이썬 스레딩. (0) | 2021.01.08 |
---|---|
NaN의 비트 패턴이 실제로 하드웨어에 의존합니까? (0) | 2021.01.08 |
전체 내역없이 시드 / 킥 스타트 프로젝트를 복제하는 방법은 무엇입니까? (0) | 2021.01.08 |
세분화 오류의 일반적인 원인에 대한 명확한 목록 (0) | 2021.01.08 |
JavaScript ES6 클래스가 비동기 코드베이스와 함께 사용됩니까? (0) | 2021.01.08 |