@@ -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()
@@ -1107,8 +1117,8 @@ example of when ``KEEP`` is needed).
11071117
11081118.. _enum-class-differences :
11091119
1110- How are Enums different?
1111- ------------------------
1120+ How are Enums and Flags different?
1121+ ----------------------------------
11121122
11131123Enums have a custom metaclass that affects many aspects of both derived :class: `Enum `
11141124classes and their instances (members).
@@ -1125,6 +1135,13 @@ responsible for ensuring that various other methods on the final :class:`Enum`
11251135class are correct (such as :meth: `__new__ `, :meth: `__getnewargs__ `,
11261136:meth: `__str__ ` and :meth: `__repr__ `).
11271137
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.
11281145
11291146Enum Members (aka instances)
11301147^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1134,9 +1151,35 @@ The most interesting thing about enum members is that they are singletons.
11341151and then puts a custom :meth: `__new__ ` in place to ensure that no new ones are
11351152ever instantiated by returning only the existing member instances.
11361153
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+
11371177
11381178.. _enum-cookbook :
11391179
1180+ Enum Cookbook
1181+ -------------
1182+
11401183
11411184While :class: `Enum `, :class: `IntEnum `, :class: `StrEnum `, :class: `Flag `, and
11421185:class: `IntFlag ` are expected to cover the majority of use-cases, they cannot
0 commit comments