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"
enum.IntEnum¶
Backported from version 3.11:
str()andformat()
IntEnum is the same as Enum, but its members are also integers and can be used anywhere that an integer can be used.
Note:
__str__()is nowint.__str__()to better support the replacement of existing constants use-case.__format__()was alreadyint.__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¶
Backported from version 3.11:
str()andformat()
IntFlag is the same as Flag, but its members are also integers and can be used anywhere that an integer can be used.
None:
__str__()is nowint.__str__()to better support the replacement of existing constants use-case.__format__()was alreadyint.__format__()for that same reason.
enum.ReprEnum¶
Backported from version 3.11
ReprEnum uses the repr() of Enum, but the str() of the mixed-in data type. The class is used for any builtin type enum.