# An attempt at a simple type safe python enum

There have been many posts here about enums in python, but none seem to be both type safe and simple. Here is my attempt at it. Please let me know if you see anything obviously wrong with it.

def typeSafeEnum(enumName, *sequential):
topLevel = type(enumName, (object,), dict(zip(sequential, [None] * len(sequential))))
vals = map(lambda x: type(x,(topLevel,),{}), sequential)
for k, v in zip(sequential, vals):
setattr(topLevel, k, v())


Then to test it:

Colors = typeSafeEnum('Colors', 'RED', 'GREEN', 'BLUE')

if __name__ == '__main__':
x = Colors.RED
assert isinstance(x, Colors)


## migrated from stackoverflow.comMar 30 '13 at 13:35

This question came from our site for professional and enthusiast programmers.

• What do you mean by "type safe"? – Armin Rigo Mar 30 '13 at 11:46
• Good point. It should not be comparable to other types. E.g., you should not be able to accidentally use an int instead of Colors.RED – user1094206 Mar 30 '13 at 11:51
• Note for posterity: as of Python 3.4, there is a standard enum module and it's available via pip install enum34 for older versions. – o11c Jul 16 '15 at 19:57

def typeSafeEnum(enumName, *sequential):


Python convention is lowercase_with_underscores for functions and parameters. Although since this is a type factory its not totally clear what convention should appply. I also wonder if passing the enum values as a list might be better.

    topLevel = type(enumName, (object,), dict(zip(sequential, [None] * len(sequential))))


There is no point in the dict you are passing since you just setattr all the enums in place already. Here you set all the enum values up with None initially and then fill them in. Why? I can also see why you call this topLevel but its not an immeadiately obvious name

    vals = map(lambda x: type(x,(topLevel,),{}), sequential)


List comprehensions are generally preffered to calling map with a lambda. But also, why? Just do this in the for loop. You don't gain anything by having it done in a map and then zipping over it.

    for k, v in zip(sequential, vals):


I suggest longer less abbreviated names

        setattr(topLevel, k, v())

• Thanks, Winston! I will do some revisions and post a new version. Besides the style and code cleanliness, does the approach seem correct to you? – user1094206 Apr 1 '13 at 15:14
• @user1094206, I avoid enums generally in favor of actually defining classes for what would be an enum value. That way I can take advantage of polymorphism and such. So stylistically, I wouldn't use this approach at all. – Winston Ewert Apr 1 '13 at 15:30

This seems like a problem to me:

>>> type(Colors.RED) is type(Colors.GREEN)
False


I'm not sure why you would want to use a Java idiom in Python. The problems TypeSafe Enums fix in Java are not really issues in Python as far as I can tell.

If you can use a regular enumeration you can use collections.namedtuple.

from collections import namedtuple
Colors = namedtuple('Colors', ['RED', 'GREEN', 'BLUE'])._make(xrange(3))