My name is Ram Rachum, and I'm a Python software developer based in Israel.

This is my personal blog. I write about technology, Python, programming and a bunch of other things.

GitHub profile

Personal Website

10th June 2011

Text

Why don’t people use enum in Python?

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:

http://packages.python.org/flufl.enum/docs/using.html#creating-an-enum

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 EnumField (or ChoiceField or whatever you want to call it) instead of the current ugly way of making an IntegerField and defining CHOICES separately.

Tagged: planetpython

Comments
All content in this website is copyright © 1986-2011 Ram Rachum.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License, with attribution to "Ram Rachum at ram.rachum.com" including link to ram.rachum.com.
To view a copy of this license, visit: http://creativecommons.org/licenses/by-sa/3.0/