How Should I Best Emulate And/or Avoid Enum's In Python?
Solution 1:
Enums have been proposed for inclusion into the language before, but were rejected (see http://www.python.org/dev/peps/pep-0354/), though there are existing packages you could use instead of writing your own implementation:
- enum: http://pypi.python.org/pypi/enum
- SymbolType (not quite the same as enums, but still useful): http://pypi.python.org/pypi/SymbolType
- Or just do a search
Solution 2:
The most common enum case is enumerated values that are part of a State or Strategy design pattern. The enums are specific states or specific optional strategies to be used. In this case, they're almost always part and parcel of some class definition
classDoTheNeedful( object ):
ONE_CHOICE = 1
ANOTHER_CHOICE = 2
YET_ANOTHER = 99def__init__( self, aSelection ):
assert aSelection in ( self.ONE_CHOICE, self.ANOTHER_CHOICE, self.YET_ANOTHER )
self.selection= aSelection
Then, in a client of this class.
dtn = DoTheNeeful( DoTheNeeful.ONE_CHOICE )
Solution 3:
There's a lot of good discussion here.
Solution 4:
What I see more often is this, in top-level module context:
FOO_BAR = 'FOO_BAR'FOO_BAZ = 'FOO_BAZ'FOO_QUX = 'FOO_QUX'
...and later...
if something is FOO_BAR: pass# do something hereelif something is FOO_BAZ: pass# do something elseelif something is FOO_QUX: pass# do something elseelse: raise Exception('Invalid value for something')
Note that the use of is
rather than ==
is taking a risk here -- it assumes that folks are using your_module.FOO_BAR
rather than the string 'FOO_BAR'
(which will normally be interned such that is
will match, but that certainly can't be counted on), and so may not be appropriate depending on context.
One advantage of doing it this way is that by looking anywhere a reference to that string is being stored, it's immediately obvious where it came from; FOO_BAZ
is much less ambiguous than 2
.
Besides that, the other thing that offends my Pythonic sensibilities re the class you propose is the use of split()
. Why not just pass in a tuple, list or other enumerable to start with?
Solution 5:
The builtin way to do enums is:
(FOO, BAR, BAZ) = range(3)
which works fine for small sets, but has some drawbacks:
- you need to count the number of elements by hand
- you can't skip values
- if you add one name, you also need to update the range number
For a complete enum implementation in python, see: http://code.activestate.com/recipes/67107/
Post a Comment for "How Should I Best Emulate And/or Avoid Enum's In Python?"