Base class for creating enumerated constants that can be combined using
the bitwise operators without losing their IntFlag membership.
IntFlag members are also subclasses of int. (Notes)
An enumeration with the values CONTINUOUS, NAMED_FLAGS, and
UNIQUE, for use with verify() to ensure various constraints
are met by a given enumeration.
An enumeration with the values STRICT, CONFORM, EJECT, and
KEEP which allows for more fine-grained control over how invalid values
are dealt with in an enumeration.
Instances are replaced with an appropriate value for Enum members.
StrEnum defaults to the lower-cased version of the member name,
while other Enums default to 1 and increase from there.
Modify the str() and repr() of an enum
to show its members as belonging to the module instead of its class,
and export the enum members to the global namespace.
Like built-in bin(), except negative values are represented in
two’s complement, and the leading bit always indicates sign
(0 implies positive, 1 implies negative).
Added in version 3.6: Flag, IntFlag, auto
Added in version 3.11: StrEnum, EnumCheck, ReprEnum, FlagBoundary, property, member, nonmember, global_enum, show_flag_values
EnumType is the metaclass for enum enumerations. It is possible
to subclass EnumType – see Subclassing EnumType
for details.
EnumType is responsible for setting the correct __repr__(),
__str__(), __format__(), and __reduce__() methods on the
final enum, as well as creating the enum members, properly handling
duplicates, providing iteration over the enum class, etc.
Added in version 3.11: Before 3.11 EnumType was called EnumMeta, which is still available as an alias.
Member values can be anything: int, str, etc. If
the exact value is unimportant you may use auto instances and an
appropriate value will be chosen for you. See auto for the
details.
While mutable/unhashable values, such as dict, list or
a mutable dataclass, can be used, they will have a
quadratic performance impact during creation relative to the
total number of mutable/unhashable values in the enum.
No longer used, kept for backward compatibility.
(class attribute, removed during class creation).
The _order_ attribute can be provided to help keep Python 2 / Python 3 code in sync.
It will be checked against the actual order of the enumeration and raise an error if the two do not match:
>>> classColor(Enum):... _order_='RED GREEN BLUE'... RED=1... BLUE=3... GREEN=2...Traceback (most recent call last):...TypeError: member order does not match _order_: ['RED', 'BLUE', 'GREEN'] ['RED', 'GREEN', 'BLUE']
Note
In Python 2 code the _order_ attribute is necessary as definition
order is lost before it can be recorded.
_ignore_ is only used during creation and is removed from the
enumeration once creation is complete.
_ignore_ is a list of names that will not become members, and whose
names will also be removed from the completed enumeration. See
TimePeriod for an example.
By default, doesn’t exist. If specified, either in the enum class
definition or in a mixin class (such as int), all values given
in the member assignment will be passed; e.g.
IntEnum is the same as Enum, but its members are also integers and can be
used anywhere that an integer can be used. If any integer operation is performed
with an IntEnum member, the resulting value loses its enumeration status.
Using auto with IntEnum results in integers of increasing
value, starting with 1.
Changed in version 3.11: __str__() is now int.__str__() to
better support the replacement of existing constants use-case.
__format__() was already int.__format__() for that same reason.
StrEnum is the same as Enum, but its members are also strings and
can be used in most of the same places that a string can be used. The result
of any string operation performed on or with a StrEnum member is not part
of the enumeration.
There are places in the stdlib that check for an exact str
instead of a str subclass (i.e. type(unknown)==str
instead of isinstance(unknown,str)), and in those locations you
will need to use str(MyStrEnum.MY_MEMBER).
Note
Using auto with StrEnum results in the lower-cased member
name as the value.
Note
__str__() is str.__str__() to better support the
replacement of existing constants use-case. __format__() is likewise
str.__format__() for that same reason.
Flag is the same as Enum, but its members support the bitwise
operators & (AND), | (OR), ^ (XOR), and ~ (INVERT);
the results of those operations are (aliases of) members of the enumeration.
If any integer operation is performed with an IntFlag member, the result is
not an IntFlag:
>>> Color.RED+23
If a Flag operation is performed with an IntFlag member and:
the result is a valid IntFlag: an IntFlag is returned
the result is not a valid IntFlag: the result depends on the FlagBoundary setting
The repr() of unnamed zero-valued flags has changed. It is now:
>>> Color(0)<Color: 0>
Note
Using auto with IntFlag results in integers that are powers
of two, starting with 1.
Changed in version 3.11: __str__() is now int.__str__() to better support the
replacement of existing constants use-case. __format__() was
already int.__format__() for that same reason.
Inversion of an IntFlag now returns a positive value that is the
union of all flags not in the given flag, rather than a negative value.
This matches the existing Flag behavior.
Ensure that any flag groups/masks contain only named flags – useful when
values are specified instead of being generated by auto():
>>> fromenumimportFlag,verify,NAMED_FLAGS>>> @verify(NAMED_FLAGS)... classColor(Flag):... RED=1... GREEN=2... BLUE=4... WHITE=15... NEON=31Traceback (most recent call last):...ValueError: invalid Flag 'Color': aliases WHITE and NEON are missing combined values of 0x18 [use enum.show_flag_values(value) for details]
Note
CONTINUOUS and NAMED_FLAGS are designed to work with integer-valued members.
EnumDict is a subclass of dict that is used as the namespace
for defining enum classes (see Preparing the class namespace).
It is exposed to allow subclasses of EnumType with advanced
behavior like having multiple values per member.
It should be called with the name of the enum class being created, otherwise
private names and internal classes will not be handled correctly.
Note that only the MutableMapping interface
(__setitem__() and update()) is overridden.
It may be possible to bypass the checks using other dict
operations like |=.
__members__ is a read-only ordered mapping of member_name:member
items. It is only available on the class.
__new__(), if specified, must create and return the enum members;
it is also a very good idea to set the member’s _value_ appropriately.
Once all the members are created it is no longer used.
auto can be used in place of a value. If used, the Enum machinery will
call an Enum’s _generate_next_value_() to get an appropriate value.
For Enum and IntEnum that appropriate value will be the highest value seen
plus one; for Flag and IntFlag it will be the first power-of-two greater
than the highest value seen; for StrEnum it will be the lower-cased version of
the member’s name. Care must be taken if mixing auto() with manually
specified values.
auto instances are only resolved when at the top level of an assignment, either by
itself or as part of a tuple:
FIRST=auto() will work (auto() is replaced with 1);
SECOND=auto(),-2 will work (auto is replaced with 2, so 2,-2 is
used to create the SECOND enum member;
THIRD=[auto(),-3] will not work ([<autoinstance>,-3] is used to
create the THIRD enum member)
Changed in version 3.11.1: In prior versions, auto() had to be the only thing
on the assignment line to work properly.
_generate_next_value_ can be overridden to customize the values used by
auto.
Note
In version 3.13 the default _generate_next_value_ will always return
the highest member value incremented by 1, and will fail if any
member is an incompatible type.
A decorator similar to the built-in @property, but specifically for
enumerations. It allows member attributes to have the same names as members
themselves.
Note
The property and the member must be defined in separate classes;
for example, the value and name attributes are defined in the
Enum class, and Enum subclasses can define members with the
names value and name.
A class decorator specifically for enumerations. It searches an
enumeration’s __members__, gathering any aliases it finds; if any are
found ValueError is raised with the details:
>>> fromenumimportEnum,unique>>> @unique... classMistake(Enum):... ONE=1... TWO=2... THREE=3... FOUR=3...Traceback (most recent call last):...ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
A class decorator specifically for enumerations. Members from
EnumCheck are used to specify which constraints should be checked
on the decorated enumeration.
A decorator to change the str() and repr() of an enum
to show its members as belonging to the module instead of its class.
Should only be used when the enum members are exported
to the module global namespace (see re.RegexFlag for an example).
Like built-in bin(), except negative values are represented in
two’s complement, and the leading bit always indicates sign
(0 implies positive, 1 implies negative).
>>> importenum>>> enum.bin(10)'0b0 1010'>>> enum.bin(~10)# ~10 is -11'0b1 0101'
The Python Software Foundation is a non-profit corporation.
Please donate.
Last updated on Nov 12, 2026 (16:22 UTC).
Found a bug?
Created using Sphinx 9.1.1.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.