Python suggest change

Enum

Enums were added to Python in version 3.4 by PEP 435.

Creating an enum

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

Iterating an enum

class Color(Enum):
    red = 1
    green = 2
    blue = 3

[c for c in Color]  # [<Color.red: 1>, <Color.green: 2>, <Color.blue: 3>]

Enums before 3.4

To use enums in Python less than 3.4, you can use enum34 package:

pip install enum34

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents