@@ -44,6 +44,11 @@ helper, :class:`auto`.
4444 Base class for creating enumerated constants that are also
4545 subclasses of :class: `int `.
4646
47+ .. class :: StrEnum
48+
49+ Base class for creating enumerated constants that are also
50+ subclasses of :class: `str `.
51+
4752.. class :: IntFlag
4853
4954 Base class for creating enumerated constants that can be combined using
@@ -601,6 +606,30 @@ However, they still can't be compared to standard :class:`Enum` enumerations::
601606 [0, 1]
602607
603608
609+ StrEnum
610+ ^^^^^^^
611+
612+ The second variation of :class: `Enum ` that is provided is also a subclass of
613+ :class: `str `. Members of a :class: `StrEnum ` can be compared to strings;
614+ by extension, string enumerations of different types can also be compared
615+ to each other. :class: `StrEnum ` exists to help avoid the problem of getting
616+ an incorrect member::
617+
618+ >>> class Directions(StrEnum):
619+ ... NORTH = 'north', # notice the trailing comma
620+ ... SOUTH = 'south'
621+
622+ Before :class: `StrEnum `, ``Directions.NORTH `` would have been the :class: `tuple `
623+ ``('north',) ``.
624+
625+ .. note ::
626+
627+ Unlike other Enum's, ``str(StrEnum.member) `` will return the value of the
628+ member instead of the usual ``"EnumClass.member" ``.
629+
630+ .. versionadded :: 3.10
631+
632+
604633IntFlag
605634^^^^^^^
606635
@@ -901,6 +930,32 @@ Using an auto-numbering :meth:`__new__` would look like::
901930 >>> Color.GREEN.value
902931 2
903932
933+ To make a more general purpose ``AutoNumber ``, add ``*args `` to the signature::
934+
935+ >>> class AutoNumber(NoValue):
936+ ... def __new__(cls, *args): # this is the only change from above
937+ ... value = len(cls.__members__) + 1
938+ ... obj = object.__new__(cls)
939+ ... obj._value_ = value
940+ ... return obj
941+ ...
942+
943+ Then when you inherit from ``AutoNumber `` you can write your own ``__init__ ``
944+ to handle any extra arguments::
945+
946+ >>> class Swatch(AutoNumber):
947+ ... def __init__(self, pantone='unknown'):
948+ ... self.pantone = pantone
949+ ... AUBURN = '3497'
950+ ... SEA_GREEN = '1246'
951+ ... BLEACHED_CORAL = () # New color, no Pantone code yet!
952+ ...
953+ >>> Swatch.SEA_GREEN
954+ <Swatch.SEA_GREEN: 2>
955+ >>> Swatch.SEA_GREEN.pantone
956+ '1246'
957+ >>> Swatch.BLEACHED_CORAL.pantone
958+ 'unknown'
904959
905960.. note ::
906961
@@ -1132,6 +1187,20 @@ all-uppercase names for members)::
11321187.. versionchanged :: 3.5
11331188
11341189
1190+ Creating members that are mixed with other data types
1191+ """""""""""""""""""""""""""""""""""""""""""""""""""""
1192+
1193+ When subclassing other data types, such as :class: `int ` or :class: `str `, with
1194+ an :class: `Enum `, all values after the `= ` are passed to that data type's
1195+ constructor. For example::
1196+
1197+ >>> class MyEnum(IntEnum):
1198+ ... example = '11', 16 # '11' will be interpreted as a hexadecimal
1199+ ... # number
1200+ >>> MyEnum.example
1201+ <MyEnum.example: 17>
1202+
1203+
11351204Boolean value of ``Enum `` classes and members
11361205"""""""""""""""""""""""""""""""""""""""""""""
11371206
@@ -1179,3 +1248,13 @@ all named flags and all named combinations of flags that are in the value::
11791248 >>> Color(7) # not named combination
11801249 <Color.CYAN|MAGENTA|BLUE|YELLOW|GREEN|RED: 7>
11811250
1251+ ``StrEnum `` and :meth: `str.__str__ `
1252+ """""""""""""""""""""""""""""""""""
1253+
1254+ An important difference between :class: `StrEnum ` and other Enums is the
1255+ :meth: `__str__ ` method; because :class: `StrEnum ` members are strings, some
1256+ parts of Python will read the string data directly, while others will call
1257+ :meth: `str() `. To make those two operations have the same result,
1258+ :meth: `StrEnum.__str__ ` will be the same as :meth: `str.__str__ ` so that
1259+ ``str(StrEnum.member) == StrEnum.member `` is true.
1260+
0 commit comments