Search This Blog

Enum in python

  • Before Python 3.4 (PEP 435),

    class Animal:
    DOG = 1
    CAT = 2

    x = Animal.DOG
    or

    class Animal:
    DOG, CAT = range(2)

    x = Animal.DOG
  • Since Python 3.4 (PEP 435),

    class Animal(Enum):
    DOG = 1
    CAT = 2

    x = Animal.DOG
    or equivalently:

    from enum import Enum

    Animal = Enum('Animal', 'DOG CAT')

    x = Animal.DOG

No comments:

Post a Comment