@@ -173,6 +173,7 @@ yourself some work and use :func:`auto()` for the values::
173
173
... FRIDAY = auto()
174
174
... SATURDAY = auto()
175
175
... SUNDAY = auto()
176
+ ... WEEKEND = SATURDAY | SUNDAY
176
177
177
178
178
179
.. _enum-advanced-tutorial :
@@ -305,6 +306,10 @@ Iterating over the members of an enum does not provide the aliases::
305
306
306
307
>>> list(Shape)
307
308
[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
309
+ >>> list(Weekday)
310
+ [<Weekday.MONDAY: 1>, <Weekday.TUESDAY: 2>, <Weekday.WEDNESDAY: 4>, <Weekday.THURSDAY: 8>, <Weekday.FRIDAY: 16>, <Weekday.SATURDAY: 32>, <Weekday.SUNDAY: 64>]
311
+
312
+ Note that the aliases ``Shape.ALIAS_FOR_SQUARE `` and ``Weekday.WEEKEND `` aren't shown.
308
313
309
314
The special attribute ``__members__ `` is a read-only ordered mapping of names
310
315
to members. It includes all names defined in the enumeration, including the
@@ -324,6 +329,11 @@ the enumeration members. For example, finding all the aliases::
324
329
>>> [name for name, member in Shape.__members__.items() if member.name != name]
325
330
['ALIAS_FOR_SQUARE']
326
331
332
+ .. note ::
333
+
334
+ Aliases for flags include values with multiple flags set, such as ``3 ``,
335
+ and no flags set, i.e. ``0 ``.
336
+
327
337
328
338
Comparisons
329
339
-----------
@@ -751,7 +761,7 @@ flags being set, the boolean evaluation is :data:`False`::
751
761
False
752
762
753
763
Individual flags should have values that are powers of two (1, 2, 4, 8, ...),
754
- while combinations of flags won't ::
764
+ while combinations of flags will not ::
755
765
756
766
>>> class Color(Flag):
757
767
... RED = auto()
@@ -1096,8 +1106,8 @@ example of when ``KEEP`` is needed).
1096
1106
1097
1107
.. _enum-class-differences :
1098
1108
1099
- How are Enums different?
1100
- ------------------------
1109
+ How are Enums and Flags different?
1110
+ ----------------------------------
1101
1111
1102
1112
Enums have a custom metaclass that affects many aspects of both derived :class: `Enum `
1103
1113
classes and their instances (members).
@@ -1114,6 +1124,13 @@ responsible for ensuring that various other methods on the final :class:`Enum`
1114
1124
class are correct (such as :meth: `__new__ `, :meth: `__getnewargs__ `,
1115
1125
:meth: `__str__ ` and :meth: `__repr__ `).
1116
1126
1127
+ Flag Classes
1128
+ ^^^^^^^^^^^^
1129
+
1130
+ Flags have an expanded view of aliasing: to be canonical, the value of a flag
1131
+ needs to be a power-of-two value, and not a duplicate name. So, in addition to the
1132
+ :class: `Enum ` definition of alias, a flag with no value (a.k.a. ``0 ``) or with more than one
1133
+ power-of-two value (e.g. ``3 ``) is considered an alias.
1117
1134
1118
1135
Enum Members (aka instances)
1119
1136
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1123,9 +1140,35 @@ The most interesting thing about enum members is that they are singletons.
1123
1140
and then puts a custom :meth: `__new__ ` in place to ensure that no new ones are
1124
1141
ever instantiated by returning only the existing member instances.
1125
1142
1143
+ Flag Members
1144
+ ^^^^^^^^^^^^
1145
+
1146
+ Flag members can be iterated over just like the :class: `Flag ` class, and only the
1147
+ canonical members will be returned. For example::
1148
+
1149
+ >>> list(Color)
1150
+ [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]
1151
+
1152
+ (Note that ``BLACK ``, ``PURPLE ``, and ``WHITE `` do not show up.)
1153
+
1154
+ Inverting a flag member returns the corresponding positive value,
1155
+ rather than a negative value --- for example::
1156
+
1157
+ >>> ~Color.RED
1158
+ <Color.GREEN|BLUE: 6>
1159
+
1160
+ Flag members have a length corresponding to the number of power-of-two values
1161
+ they contain. For example::
1162
+
1163
+ >>> len(Color.PURPLE)
1164
+ 2
1165
+
1126
1166
1127
1167
.. _enum-cookbook :
1128
1168
1169
+ Enum Cookbook
1170
+ -------------
1171
+
1129
1172
1130
1173
While :class: `Enum `, :class: `IntEnum `, :class: `StrEnum `, :class: `Flag `, and
1131
1174
:class: `IntFlag ` are expected to cover the majority of use-cases, they cannot
@@ -1299,7 +1342,7 @@ enumerations)::
1299
1342
DuplicateFreeEnum
1300
1343
^^^^^^^^^^^^^^^^^
1301
1344
1302
- Raises an error if a duplicate member name is found instead of creating an
1345
+ Raises an error if a duplicate member value is found instead of creating an
1303
1346
alias::
1304
1347
1305
1348
>>> class DuplicateFreeEnum(Enum):
0 commit comments