diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2020-09-22 00:23:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-22 00:23:13 (GMT) |
commit | 0063ff4e583505e69473caa978e476ea4c559b83 (patch) | |
tree | 637c86f3b68e8d63678fb666c5cbf4e3326783c6 /Doc/library | |
parent | 68526fe258da8c01196fd7cf48e8e5f1280bf8fd (diff) | |
download | cpython-0063ff4e583505e69473caa978e476ea4c559b83.zip cpython-0063ff4e583505e69473caa978e476ea4c559b83.tar.gz cpython-0063ff4e583505e69473caa978e476ea4c559b83.tar.bz2 |
bpo-41816: add `StrEnum` (GH-22337)
`StrEnum` ensures that its members were already strings, or intended to
be strings.
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/enum.rst | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 2f84be2..843d961 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -44,6 +44,11 @@ helper, :class:`auto`. Base class for creating enumerated constants that are also subclasses of :class:`int`. +.. class:: StrEnum + + Base class for creating enumerated constants that are also + subclasses of :class:`str`. + .. class:: IntFlag Base class for creating enumerated constants that can be combined using @@ -601,6 +606,25 @@ However, they still can't be compared to standard :class:`Enum` enumerations:: [0, 1] +StrEnum +^^^^^^^ + +The second variation of :class:`Enum` that is provided is also a subclass of +:class:`str`. Members of a :class:`StrEnum` can be compared to strings; +by extension, string enumerations of different types can also be compared +to each other. :class:`StrEnum` exists to help avoid the problem of getting +an incorrect member:: + + >>> class Directions(StrEnum): + ... NORTH = 'north', # notice the trailing comma + ... SOUTH = 'south' + +Before :class:`StrEnum`, ``Directions.NORTH`` would have been the :class:`tuple` +``('north',)``. + +.. versionadded:: 3.10 + + IntFlag ^^^^^^^ @@ -1132,6 +1156,20 @@ all-uppercase names for members):: .. versionchanged:: 3.5 +Creating members that are mixed with other data types +""""""""""""""""""""""""""""""""""""""""""""""""""""" + +When subclassing other data types, such as :class:`int` or :class:`str`, with +an :class:`Enum`, all values after the `=` are passed to that data type's +constructor. For example:: + + >>> class MyEnum(IntEnum): + ... example = '11', 16 # '11' will be interpreted as a hexadecimal + ... # number + >>> MyEnum.example + <MyEnum.example: 17> + + Boolean value of ``Enum`` classes and members """"""""""""""""""""""""""""""""""""""""""""" |