File tree Expand file tree Collapse file tree 1 file changed +19
-4
lines changed Expand file tree Collapse file tree 1 file changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -504,16 +504,31 @@ Data Types
504
504
505
505
.. class :: StrEnum
506
506
507
- ``StrEnum `` is the same as :class: `Enum `, but its members are also strings and can be used
508
- in most of the same places that a string can be used. The result of any string
509
- operation performed on or with a *StrEnum * member is not part of the enumeration.
507
+ *StrEnum * is the same as :class: `Enum `, but its members are also strings and
508
+ can be used in most of the same places that a string can be used. The result
509
+ of any string operation performed on or with a *StrEnum * member is not part
510
+ of the enumeration.
511
+
512
+ >>> from enum import StrEnum, auto
513
+ >>> class Color (StrEnum ):
514
+ ... RED = ' r'
515
+ ... GREEN = ' g'
516
+ ... BLUE = ' b'
517
+ ... UNKNOWN = auto()
518
+ ...
519
+ >>> Color.RED
520
+ <Color.RED: 'r'>
521
+ >>> Color.UNKNOWN
522
+ <Color.UNKNOWN: 'unknown'>
523
+ >>> str (Color.UNKNOWN )
524
+ 'unknown'
510
525
511
526
.. note ::
512
527
513
528
There are places in the stdlib that check for an exact :class: `str `
514
529
instead of a :class: `str ` subclass (i.e. ``type(unknown) == str ``
515
530
instead of ``isinstance(unknown, str) ``), and in those locations you
516
- will need to use ``str(StrEnum.member ) ``.
531
+ will need to use ``str(MyStrEnum.MY_MEMBER ) ``.
517
532
518
533
.. note ::
519
534
You can’t perform that action at this time.
0 commit comments