I'm relatively new to Python and I was interested in finding out the simplest way to create an enumeration.
The best I've found is something like:
(APPLE, BANANA, WALRUS) = range(3)
Which sets APPLE to 0, BANANA to 1, etc.
But I'm wondering if there's a simpler way.
Enums were added in python 3.4 (docs). See PEP 0435 for details.
If you are on python 2.x, there exists a backport on pypi.
Your usage example is most similar to the python enum's functional API:
However, this is a more typical usage example:
You can also use an
IntEnum
if you need enum instances to compare equal with integers, but I don't recommend this unless there is a good reason you need that behaviour.