Skip to content

PyBackport: Enumerations

py_back allows using the enum module just as the original, with the only difference at the import.

from py_back import enum


class Number(enum.IntEnum):
    """Enumeration using the original 'IntEnum' call"""
    ONE = enum.auto()
    TWO = 2


class Animal(enum.StrEnum):
    """Supported original 'StrEnum' for python versions < 3.11"""
    CAT = enum.auto()
    DOG = "dog"


class Color(enum.TupleEnum):
    """Experimental 'TupleEnum' class"""
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)

Backported classes

enum.IntEnum

IntEnum is the same as Enum, but its members are also integers and can be used anywhere that an integer can be used.

Backported from version 3.11: __str__() is now int.__str__() to better support the replacement of existing constants use-case. __format__() was already int.__format__() for that same reason.

enum.StrEnum

Backported from version 3.11
StrEnum is the same as Enum, but its members are also strings and can be used in most of the same places that a string can be used.

>>> from py_back import enum
>>> class Animal(enum.StrEnum):
...    CAT = enum.auto()
...    DOG = "dog"
...
>>> Animal.CAT
cat
>>> Animal.DOG.title()
'Dog'
>>> Animal.CAT == "cat"
True
>>> Animal.CAT + Animal.DOG
'catdog'
>>> " and ".join(list(Animals))
'cat and dog'

Note: Using auto results in the lower-cased member name as the value.

enum.IntFlag

IntFlag is the same as Flag, but its members are also integers and can be used anywhere that an integer can be used.

Backported from version 3.11: __str__() is now int.__str__() to better support the replacement of existing constants use-case. __format__() was already int.__format__() for that same reason.

enum.ReprEnum

ReprEnum uses the repr() of Enum, but the str() of the mixed-in data type. The class is used for any builtin type enum.


Experimental classes

enum.TupleEnum

TupleEnum is the same as Enum, but its members are also tuples and can be used anywhere as tuples can be used.

>>> from py_back import enum
>>> class Color(enum.TupleEnum):
...    """Experimental 'TupleEnum' class"""
...    BLACK = (0, 0, 0)
...    WHITE = (255, 255, 255)
...
>>> Color.BLACK
(0, 0, 0)
>>> list(Color.WHITE)
[255, 255, 255]

Note: Using auto is not supported by TupleEnum, and it will raise a NotImplementedError.