Pelokalan
Python ID
id en
Masuk
Proyek
library__enum.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
:class:`EnumType`
The ``type`` for Enum and its subclasses.
Used by :class:`IntEnum`, :class:`StrEnum`, and :class:`IntFlag` to keep the :class:`str() <str>` of the mixed-in type.
A subclass of :class:`dict` for use when subclassing :class:`EnumType`.
Data types
*EnumType* is the :term:`metaclass` for *enum* enumerations. It is possible to subclass *EnumType* -- see :ref:`Subclassing EnumType <enumtype-examples>` for details.
``EnumType`` is responsible for setting the correct :meth:`!__repr__`, :meth:`!__str__`, :meth:`!__format__`, and :meth:`!__reduce__` methods on the final *enum*, as well as creating the enum members, properly handling duplicates, providing iteration over the enum class, etc.
Before 3.11 ``EnumType`` was called ``EnumMeta``, which is still available as an alias.
type
tipe
A mix-in type for the new Enum.
Before Python 3.12, a ``TypeError`` is raised if a non-Enum-member is used in a containment check.
>>> class Color(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']
There are places in the stdlib that check for an exact :class:`str` instead of a :class:`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)``.
Returns all the flags in *type(self)* that are not in *self*::
:class:`!ReprEnum` uses the :meth:`repr() <Enum.__repr__>` of :class:`Enum`, but the :class:`str() <str>` of the mixed-in data type:
Inherit from :class:`!ReprEnum` to keep the :class:`str() <str>` / :func:`format` of the mixed-in data type instead of using the :class:`Enum`-default :meth:`str() <Enum.__str__>`.
*EnumDict* is a subclass of :class:`dict` that is used as the namespace for defining enum classes (see :ref:`prepare`). It is exposed to allow subclasses of :class:`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.
:attr:`~EnumType.__members__` is a read-only ordered mapping of ``member_name``:``member`` items. It is only available on the class.
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 :keyword:`class` decorator specifically for enumerations. It searches an enumeration's :attr:`~EnumType.__members__`, gathering any aliases it finds; if any are found :exc:`ValueError` is raised with the details::
These three enum types are designed to be drop-in replacements for existing integer- and string-based values; as such, they have extra limitations:
If you do not need/want those limitations, you can either create your own base class by mixing in the ``int`` or ``str`` type yourself::