@@ -173,6 +173,7 @@ yourself some work and use :func:`auto()` for the values::
173173 ... FRIDAY = auto()
174174 ... SATURDAY = auto()
175175 ... SUNDAY = auto()
176+ ... WEEKEND = SATURDAY | SUNDAY
176177
177178
178179.. _enum-advanced-tutorial :
@@ -305,6 +306,10 @@ Iterating over the members of an enum does not provide the aliases::
305306
306307 >>> list(Shape)
307308 [<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.
308313
309314The special attribute ``__members__ `` is a read-only ordered mapping of names
310315to members. It includes all names defined in the enumeration, including the
@@ -324,6 +329,11 @@ the enumeration members. For example, finding all the aliases::
324329 >>> [name for name, member in Shape.__members__.items() if member.name != name]
325330 ['ALIAS_FOR_SQUARE']
326331
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+
327337
328338Comparisons
329339-----------
@@ -751,7 +761,7 @@ flags being set, the boolean evaluation is :data:`False`::
751761 False
752762
753763Individual 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 ::
755765
756766 >>> class Color(Flag):
757767 ... RED = auto()
@@ -1096,8 +1106,8 @@ example of when ``KEEP`` is needed).
10961106
10971107.. _enum-class-differences :
10981108
1099- How are Enums different?
1100- ------------------------
1109+ How are Enums and Flags different?
1110+ ----------------------------------
11011111
11021112Enums have a custom metaclass that affects many aspects of both derived :class: `Enum `
11031113classes and their instances (members).
@@ -1114,6 +1124,13 @@ responsible for ensuring that various other methods on the final :class:`Enum`
11141124class are correct (such as :meth: `__new__ `, :meth: `__getnewargs__ `,
11151125:meth: `__str__ ` and :meth: `__repr__ `).
11161126
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.
11171134
11181135Enum Members (aka instances)
11191136^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1123,9 +1140,35 @@ The most interesting thing about enum members is that they are singletons.
11231140and then puts a custom :meth: `__new__ ` in place to ensure that no new ones are
11241141ever instantiated by returning only the existing member instances.
11251142
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+
11261166
11271167.. _enum-cookbook :
11281168
1169+ Enum Cookbook
1170+ -------------
1171+
11291172
11301173While :class: `Enum `, :class: `IntEnum `, :class: `StrEnum `, :class: `Flag `, and
11311174:class: `IntFlag ` are expected to cover the majority of use-cases, they cannot
@@ -1299,7 +1342,7 @@ enumerations)::
12991342DuplicateFreeEnum
13001343^^^^^^^^^^^^^^^^^
13011344
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
13031346alias::
13041347
13051348 >>> class DuplicateFreeEnum(Enum):
0 commit comments