A while ago I was introduced to the concept of enum in Java. I never programmed Java, but this looks like the solution for when you want a variable to hold a value from a limited set of possible values. What is usually done in Python is either using a string or an index number, both of which are less elegant solutions in my opinion.
Python doesn’t provide a built-in enum type, but I found a few third-party modules, out of which I chose
flufl.enum as being the most promising:
This looks like a pretty good solution. A tad less native than in Java, but still much better than strings or ints.
So I wonder: Why do I never see Python code that uses
flufl.enum, or any other enum package for that matter? Is there some reason not to use enums in Python? Perhaps a large portion of Python programmers are ignorant about enums, (as I was before I saw it recently?) Or perhaps people just don’t really care about making their code elegant?
Anyway I’ll try to start using
flufl.enum soon. One awesome application that this could have is to make a Django
ChoiceField or whatever you want to call it) instead of the current ugly way of making an
IntegerField and defining