DEV Community

Cover image for Python Enum
Waylon Walker
Waylon Walker

Posted on • Originally published at waylonwalker.com

Python Enum

Python comes with an enum module for creating enums. You can make your own enum by inheriting importing and inheriting from Enum.

from enum import Enum


class LifeCycle(Enum):
    configure = 1
    glob = 2
    pre_render = 3
    render = 4
    post_render = 5
    save = 6
Enter fullscreen mode Exit fullscreen mode

auto incrementing

Enum values can be auto incremented by importing auto, and calling auto() as their value.

from enum import Enum, auto


class LifeCycle(Enum):
    configure = auto()
    glob = auto()
    pre_render = auto()
    render = auto()
    post_render = auto()
    save = auto()
Enter fullscreen mode Exit fullscreen mode

using the enum

Enum's are accessed directy under the class itself, and have primarily two methods underneath each thing you make, .name and .value.

Lifecycle.glob
Lifecycle.glob.value
Lifecycle.glob.name
Enter fullscreen mode Exit fullscreen mode

using the Lifecycle Enum

Discussion (0)