Enum in Python

Creating in enum in python is very easy. Enum in python are iterable and here is how you can create enum in the programming language.

Section 7.1: Creating an enum in Python (Python 2.4 through 3.3)

Enums have been backported from Python 3.4 to Python 2.4 through Python 3.3. You can get this the enum34 backport from PyPI.

pip install enum34

Creation of an enum is identical to how it works in Python 3.4+

from enum import Enum
class Color(Enum):
red = 1
green = 2
blue = 3
print(Color.red) # Color.red
print(Color(1)) # Color.red
print(Color['red']) # Color.red

Section 7.2: Iteration

Enum s are iterable:

class Color(Enum):
red = 1
green = 2
blue = 3
[c for c in Color] # [, ,enum 

More Links:

LEAVE A REPLY

Please enter your comment!
Please enter your name here