Python Enum

Advertisement

Your Ad could be here. I want to connect my readers to relavant ads. If you have a product targeted at developers, let's talk. [email protected]

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

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()

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

using the Lifecycle Enum