What's the common practice for enums in Python?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC P Beethoven - Piano Sonata No 2 in A
--
Chapters
00:00 Question
00:26 Accepted answer (Score 375)
00:37 Answer 2 (Score 22)
01:14 Answer 3 (Score 10)
02:01 Answer 4 (Score 7)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/7028...
Question links:
[How can I represent an 'enum' in Python?]: https://stackoverflow.com/questions/3693...
Answer 1 links:
[try this recipe]: http://code.activestate.com/recipes/6710.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #enums
#avk47
--
Track title: CC P Beethoven - Piano Sonata No 2 in A
--
Chapters
00:00 Question
00:26 Accepted answer (Score 375)
00:37 Answer 2 (Score 22)
01:14 Answer 3 (Score 10)
02:01 Answer 4 (Score 7)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/7028...
Question links:
[How can I represent an 'enum' in Python?]: https://stackoverflow.com/questions/3693...
Answer 1 links:
[try this recipe]: http://code.activestate.com/recipes/6710.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #enums
#avk47
ACCEPTED ANSWER
Score 374
class Materials:
Shaded, Shiny, Transparent, Matte = range(4)
>>> print Materials.Matte
3
Update: For Python 3.4+:
As of Python 3.4+, you can now use Enum (or IntEnum for enums with int values) from the enum module. Use enum.auto to increment the values up automatically:
import enum
class Materials(enum.IntEnum):
Shaded = 1
Shiny = enum.auto()
Transparent = 3
Matte = enum.auto()
print(Materials.Shiny == 2) # True
print(Materials.Matte == 4) # True
ANSWER 2
Score 21
I've seen this pattern several times:
>>> class Enumeration(object):
def __init__(self, names): # or *names, with no .split()
for number, name in enumerate(names.split()):
setattr(self, name, number)
>>> foo = Enumeration("bar baz quux")
>>> foo.quux
2
You can also just use class members, though you'll have to supply your own numbering:
>>> class Foo(object):
bar = 0
baz = 1
quux = 2
>>> Foo.quux
2
If you're looking for something more robust (sparse values, enum-specific exception, etc.), try this recipe.
ANSWER 3
Score 9
I have no idea why Enums are not support natively by Python. The best way I've found to emulate them is by overridding _ str _ and _ eq _ so you can compare them and when you use print() you get the string instead of the numerical value.
class enumSeason():
Spring = 0
Summer = 1
Fall = 2
Winter = 3
def __init__(self, Type):
self.value = Type
def __str__(self):
if self.value == enumSeason.Spring:
return 'Spring'
if self.value == enumSeason.Summer:
return 'Summer'
if self.value == enumSeason.Fall:
return 'Fall'
if self.value == enumSeason.Winter:
return 'Winter'
def __eq__(self,y):
return self.value==y.value
Usage:
>>> s = enumSeason(enumSeason.Spring)
>>> print(s)
Spring
ANSWER 4
Score 6
You could probably use an inheritance structure although the more I played with this the dirtier I felt.
class AnimalEnum:
@classmethod
def verify(cls, other):
return issubclass(other.__class__, cls)
class Dog(AnimalEnum):
pass
def do_something(thing_that_should_be_an_enum):
if not AnimalEnum.verify(thing_that_should_be_an_enum):
raise OhGodWhy