enums - What's the cleanest way to set up an enumeration in Python? -
this question has answer here:
- how can represent 'enum' in python? 43 answers
i'm relatively new python , interested in finding out simplest way create enumeration.
the best i've found like:
(apple, banana, walrus) = range(3)
which sets apple 0, banana 1, etc.
but i'm wondering if there's simpler way.
enums added in python 3.4 (docs). see pep 0435 details.
if on python 2.x, there exists backport on pypi.
pip install enum34
your usage example similar python enum's functional api:
>>> enum import enum >>> myenum = enum('myenum', 'apple banana walrus') >>> myenum.banana <myenum.banana: 2>
however, more typical usage example:
class myenum(enum): apple = 1 banana = 2 walrus = 3
you can use intenum
if need enum instances compare equal integers, don't recommend unless there reason need behaviour.
Comments
Post a Comment