Skip to content

Instantly share code, notes, and snippets.

@sneeu
Created August 15, 2013 17:10
Show Gist options
  • Save sneeu/6242605 to your computer and use it in GitHub Desktop.
Save sneeu/6242605 to your computer and use it in GitHub Desktop.
def enum(name, attrs):
"""
>>> Fruit = enum('Fruit', 'apple banana cranberry')
>>> Fruit.apple #doctest: +ELLIPSIS
<object object at 0x...>
>>> Fruit.apple == Fruit.banana
False
>>> Animal = enum('Animal', 'cat dog elephant')
>>> Animal.cat #doctest: +ELLIPSIS
<object object at 0x...>
>>> Animal.dog == Animal.elephant
False
>>> Animal.cat == Fruit.apple
False
"""
return type(name, (object, ), {a: object() for a in attrs.split(' ')})
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment