A better Enum for Python.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
minitools
.gitignore
README.md
magic_enum.py
testing.py

README.md

Magic Enum

Magic Enum is a better Enum for Python.

An Enum can be initialized like either of these:

class TrafficLight(Enum):
    red
    amber
    green
    
class Colour(Enum):
    red, blue, green, yellow

Enum constants have all the goodies you'd expect from a regular enum:

>>> Colour.red == Colour.blue
False

>> Colour.red == Colour.red
True

But they've also got some tricks up their sleeves:

>>> for c in Colour:
...     print(c)
... 
Colour.red
Colour.blue
Colour.green
Colour.yellow

Like this one, inspired by the C++ enum's increment overloading:

>>> next(TrafficLight.red)
TrafficLight.amber

They've got a type that makes sense:

>>> type(Colour.blue) == Colour
True
>>> type(Colour.blue)
<class 'Colour' at 0x7fe78a8000e8>

And the type is meaningful, too:

>>> Colour.red != TrafficLight.red
True

If you like, you can add values to the enum:

class CarBrand(Enum):
    Ford = 1
    Toyota = 3
    Mitsubishi = 2


>>> for brand in CarBrand: print(brand)
CarBrand.Ford(value=1)
CarBrand.Mitsubishi(value=2)
CarBrand.Toyota(value=3)

Note that the results will be returned in order of their values!

(If the values aren't integers, we'll default to the order of insertion.)

In order to check if a value is one of multiple enum constants, use the built-in in operator:

>>> col = Colour.red
>>> col in (Colour.blue, Colour.green, Colour.yellow)
False
>> col in (Colour.red, Colour.yellow)
True

It's also possible to use subscripting to get an enum constant, like so:

>>> Colour["red"]
Colour.red

And more tests can be found in testing.py. Enjoy!