@@ -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()
@@ -1107,8 +1117,8 @@ example of when ``KEEP`` is needed).
1107
1117
1108
1118
.. _enum-class-differences :
1109
1119
1110
- How are Enums different?
1111
- ------------------------
1120
+ How are Enums and Flags different?
1121
+ ----------------------------------
1112
1122
1113
1123
Enums have a custom metaclass that affects many aspects of both derived :class: `Enum `
1114
1124
classes and their instances (members).
@@ -1125,6 +1135,13 @@ responsible for ensuring that various other methods on the final :class:`Enum`
1125
1135
class are correct (such as :meth: `__new__ `, :meth: `__getnewargs__ `,
1126
1136
:meth: `__str__ ` and :meth: `__repr__ `).
1127
1137
1138
+ Flag Classes
1139
+ ^^^^^^^^^^^^
1140
+
1141
+ Flags have an expanded view of aliasing: to be canonical, the value of a flag
1142
+ needs to be a power-of-two value, and not a duplicate name. So, in addition to the
1143
+ :class: `Enum ` definition of alias, a flag with no value (a.k.a. ``0 ``) or with more than one
1144
+ power-of-two value (e.g. ``3 ``) is considered an alias.
1128
1145
1129
1146
Enum Members (aka instances)
1130
1147
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1134,9 +1151,35 @@ The most interesting thing about enum members is that they are singletons.
1134
1151
and then puts a custom :meth: `__new__ ` in place to ensure that no new ones are
1135
1152
ever instantiated by returning only the existing member instances.
1136
1153
1154
+ Flag Members
1155
+ ^^^^^^^^^^^^
1156
+
1157
+ Flag members can be iterated over just like the :class: `Flag ` class, and only the
1158
+ canonical members will be returned. For example::
1159
+
1160
+ >>> list(Color)
1161
+ [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]
1162
+
1163
+ (Note that ``BLACK ``, ``PURPLE ``, and ``WHITE `` do not show up.)
1164
+
1165
+ Inverting a flag member returns the corresponding positive value,
1166
+ rather than a negative value --- for example::
1167
+
1168
+ >>> ~Color.RED
1169
+ <Color.GREEN|BLUE: 6>
1170
+
1171
+ Flag members have a length corresponding to the number of power-of-two values
1172
+ they contain. For example::
1173
+
1174
+ >>> len(Color.PURPLE)
1175
+ 2
1176
+
1137
1177
1138
1178
.. _enum-cookbook :
1139
1179
1180
+ Enum Cookbook
1181
+ -------------
1182
+
1140
1183
1141
1184
While :class: `Enum `, :class: `IntEnum `, :class: `StrEnum `, :class: `Flag `, and
1142
1185
:class: `IntFlag ` are expected to cover the majority of use-cases, they cannot
0 commit comments