@@ -23,9 +23,9 @@ by identity, and the enumeration itself can be iterated over.
2323Module Contents
2424---------------
2525
26- This module defines two enumeration classes that can be used to define unique
27- sets of names and values: :class: `Enum ` and :class: `IntEnum `. It also defines
28- one decorator, :func: `unique `.
26+ This module defines four enumeration classes that can be used to define unique
27+ sets of names and values: :class: `Enum `, :class: `IntEnum `, and
28+ :class: ` IntFlags `. It also defines one decorator, :func: `unique `.
2929
3030.. class :: Enum
3131
@@ -37,10 +37,23 @@ one decorator, :func:`unique`.
3737 Base class for creating enumerated constants that are also
3838 subclasses of :class: `int `.
3939
40+ .. class :: IntFlag
41+
42+ Base class for creating enumerated constants that can be combined using
43+ the bitwise operators without losing their :class: `IntFlag ` membership.
44+ :class: `IntFlag ` members are also subclasses of :class: `int `.
45+
46+ .. class :: Flag
47+
48+ Base class for creating enumerated constants that can be combined using
49+ the bitwise operations without losing their :class: `Flag ` membership.
50+
4051.. function :: unique
4152
4253 Enum class decorator that ensures only one name is bound to any one value.
4354
55+ .. versionadded :: 3.6 ``Flag``, ``IntFlag``
56+
4457
4558Creating an Enum
4659----------------
@@ -478,7 +491,7 @@ Derived Enumerations
478491IntEnum
479492^^^^^^^
480493
481- A variation of :class: `Enum ` is provided which is also a subclass of
494+ The first variation of :class: `Enum ` that is provided is also a subclass of
482495:class: `int `. Members of an :class: `IntEnum ` can be compared to integers;
483496by extension, integer enumerations of different types can also be compared
484497to each other::
@@ -521,13 +534,54 @@ However, they still can't be compared to standard :class:`Enum` enumerations::
521534 >>> [i for i in range(Shape.square)]
522535 [0, 1]
523536
524- For the vast majority of code, :class: `Enum ` is strongly recommended,
525- since :class: `IntEnum ` breaks some semantic promises of an enumeration (by
526- being comparable to integers, and thus by transitivity to other
527- unrelated enumerations). It should be used only in special cases where
528- there's no other choice; for example, when integer constants are
529- replaced with enumerations and backwards compatibility is required with code
530- that still expects integers.
537+
538+ IntFlag
539+ ^^^^^^^
540+
541+ The next variation of :class: `Enum ` provided, :class: `IntFlag `, is also based
542+ on :class: `int `. The difference being :class: `IntFlag ` members can be combined
543+ using the bitwise operators (&, \| , ^, ~) and the result is still an
544+ :class: `IntFlag ` member. However, as the name implies, :class: `IntFlag `
545+ members also subclass :class: `int ` and can be used wherever an :class: `int ` is.
546+ Any operation on an :class: `IntFlag ` member besides the bit-wise operations
547+ will lose the :class: `IntFlag ` membership.
548+
549+ >>> from enum import IntFlag
550+ >>> class Perm (IntFlag ):
551+ ... R = 4
552+ ... W = 2
553+ ... X = 1
554+ ...
555+ >>> Perm.R | Perm.W
556+ <Perm.R|W: 6>
557+ >>> Perm.R + Perm.W
558+ 6
559+ >>> RW = Perm.R | Perm.W
560+ >>> Perm.R in RW
561+ True
562+
563+ .. versionadded :: 3.6
564+
565+
566+ Flag
567+ ^^^^
568+
569+ The last variation is :class: `Flag `. Like :class: `IntFlag `, :class: `Flag `
570+ members can be combined using the bitwise operators (^, \| , ^, ~). Unlike
571+ :class: `IntFlag `, they cannot be combined with, nor compared against, any
572+ other :class: `Flag ` enumeration nor :class: `int `.
573+
574+ .. versionadded :: 3.6
575+
576+ .. note ::
577+
578+ For the majority of new code, :class: `Enum ` and :class: `Flag ` are strongly
579+ recommended, since :class: `IntEnum ` and :class: `IntFlag ` break some
580+ semantic promises of an enumeration (by being comparable to integers, and
581+ thus by transitivity to other unrelated enumerations). :class: `IntEnum `
582+ and :class: `IntFlag ` should be used only in cases where :class: `Enum ` and
583+ :class: `Flag ` will not do; for example, when integer constants are replaced
584+ with enumerations, or for interoperability with other systems.
531585
532586
533587Others
@@ -567,10 +621,10 @@ Some rules:
567621Interesting examples
568622--------------------
569623
570- While :class: `Enum ` and :class: `IntEnum ` are expected to cover the majority of
571- use-cases, they cannot cover them all. Here are recipes for some different
572- types of enumerations that can be used directly, or as examples for creating
573- one's own.
624+ While :class: `Enum `, :class: `IntEnum `, :class: ` IntFlag `, and :class: ` Flag ` are
625+ expected to cover the majority of use-cases, they cannot cover them all. Here
626+ are recipes for some different types of enumerations that can be used directly,
627+ or as examples for creating one's own.
574628
575629
576630AutoNumber
@@ -731,10 +785,56 @@ member instances.
731785Finer Points
732786^^^^^^^^^^^^
733787
788+ Supported ``__dunder__ `` names
789+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
790+
791+ :attr: `__members__ ` is an :class: `OrderedDict ` of ``member_name ``:``member ``
792+ items. It is only available on the class.
793+
794+ :meth: `__new__ `, if specified, must create and return the enum members; it is
795+ also a very good idea to set the member's :attr: `_value_ ` appropriately. Once
796+ all the members are created it is no longer used.
797+
798+
799+ Supported ``_sunder_ `` names
800+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
801+
802+ - ``_name_ `` -- name of the member
803+ - ``_value_ `` -- value of the member; can be set / modified in ``__new__ ``
804+
805+ - ``_missing_ `` -- a lookup function used when a value is not found; may be
806+ overridden
807+ - ``_order_ `` -- used in Python 2/3 code to ensure member order is consistent
808+ (class attribute, removed during class creation)
809+
810+ .. versionadded :: 3.6 ``_missing_``, ``_order_``
811+
812+ To help keep Python 2 / Python 3 code in sync an :attr: `_order_ ` attribute can
813+ be provided. It will be checked against the actual order of the enumeration
814+ and raise an error if the two do not match::
815+
816+ >>> class Color(Enum):
817+ ... _order_ = 'red green blue'
818+ ... red = 1
819+ ... blue = 3
820+ ... green = 2
821+ ...
822+ Traceback (most recent call last):
823+ ...
824+ TypeError: member order does not match _order_
825+
826+ .. note ::
827+
828+ In Python 2 code the :attr: `_order_ ` attribute is necessary as definition
829+ order is lost before it can be recorded.
830+
831+ ``Enum `` member type
832+ ~~~~~~~~~~~~~~~~~~~~
833+
734834:class: `Enum ` members are instances of an :class: `Enum ` class, and even
735835though they are accessible as `EnumClass.member `, they should not be accessed
736836directly from the member as that lookup may fail or, worse, return something
737- besides the :class: ` Enum ` member you looking for::
837+ besides the `` Enum ` ` member you looking for::
738838
739839 >>> class FieldTypes(Enum):
740840 ... name = 0
@@ -748,16 +848,24 @@ besides the :class:`Enum` member you looking for::
748848
749849.. versionchanged :: 3.5
750850
751- Boolean evaluation: Enum classes that are mixed with non-Enum types (such as
851+
852+ Boolean value of ``Enum `` classes and members
853+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
854+
855+ ``Enum `` members that are mixed with non-Enum types (such as
752856:class: `int `, :class: `str `, etc.) are evaluated according to the mixed-in
753- type's rules; otherwise, all members evaluate as `` True ` `. To make your own
857+ type's rules; otherwise, all members evaluate as :data: ` True `. To make your own
754858Enum's boolean evaluation depend on the member's value add the following to
755859your class::
756860
757861 def __bool__(self):
758862 return bool(self.value)
759863
760- The :attr: `__members__ ` attribute is only available on the class.
864+ ``Enum `` classes always evaluate as :data: `True `.
865+
866+
867+ ``Enum `` classes with methods
868+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
761869
762870If you give your :class: `Enum ` subclass extra methods, like the `Planet `_
763871class above, those methods will show up in a :func: `dir ` of the member,
@@ -768,30 +876,3 @@ but not of the class::
768876 >>> dir(Planet.EARTH)
769877 ['__class__', '__doc__', '__module__', 'name', 'surface_gravity', 'value']
770878
771- The :meth: `__new__ ` method will only be used for the creation of the
772- :class: `Enum ` members -- after that it is replaced. Any custom :meth: `__new__ `
773- method must create the object and set the :attr: `_value_ ` attribute
774- appropriately.
775-
776- If you wish to change how :class: `Enum ` members are looked up you should either
777- write a helper function or a :func: `classmethod ` for the :class: `Enum `
778- subclass.
779-
780- To help keep Python 2 / Python 3 code in sync a user-specified :attr: `_order_ `,
781- if provided, will be checked to ensure the actual order of the enumeration
782- matches::
783-
784- >>> class Color(Enum):
785- ... _order_ = 'red green blue'
786- ... red = 1
787- ... blue = 3
788- ... green = 2
789- ...
790- Traceback (most recent call last):
791- ...
792- TypeError: member order does not match _order_
793-
794- .. note ::
795-
796- In Python 2 code the :attr: `_order_ ` attribute is necessary as definition
797- order is lost during class creation.
0 commit comments