diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2018-01-22 15:56:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-22 15:56:37 (GMT) |
commit | a4b1bb4801f7a941ff9e86b96da539be1c288833 (patch) | |
tree | a5cd81d18b3d10dc8f8b6cdf7d51e8172d5f7ff9 /Doc/library/enum.rst | |
parent | 579e0b80b953b8a47385bc50844dbaac45d6f437 (diff) | |
download | cpython-a4b1bb4801f7a941ff9e86b96da539be1c288833.zip cpython-a4b1bb4801f7a941ff9e86b96da539be1c288833.tar.gz cpython-a4b1bb4801f7a941ff9e86b96da539be1c288833.tar.bz2 |
bpo-31801: Enum: add _ignore_ as class option (#5237)
* bpo-31801: Enum: add _ignore_ as class option
_ignore_ is a list, or white-space seperated str, of names that will not
be candidates for members; these names, and _ignore_ itself, are removed
from the final class.
* bpo-31801: Enum: add documentation for _ignore_
* bpo-31801: Enum: remove trailing whitespace
* bpo-31801: Enum: fix bulleted list format
* bpo-31801: add version added for _ignore_
Diffstat (limited to 'Doc/library/enum.rst')
-rw-r--r-- | Doc/library/enum.rst | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 5c1b226..fc65a3d 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -379,7 +379,8 @@ The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (:meth:`__str__`, -:meth:`__add__`, etc.) and descriptors (methods are also descriptors). +:meth:`__add__`, etc.), descriptors (methods are also descriptors), and +variable names listed in :attr:`_ignore_`. Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then whatever value(s) were given to the enum member will be passed into those @@ -943,6 +944,25 @@ will be passed to those methods:: 9.802652743337129 +TimePeriod +^^^^^^^^^^ + +An example to show the :attr:`_ignore_` attribute in use:: + + >>> from datetime import timedelta + >>> class Period(timedelta, Enum): + ... "different lengths of time" + ... _ignore_ = 'Period i' + ... Period = vars() + ... for i in range(367): + ... Period['day_%d' % i] = i + ... + >>> list(Period)[:2] + [<Period.day_0: datetime.timedelta(0)>, <Period.day_1: datetime.timedelta(days=1)>] + >>> list(Period)[-2:] + [<Period.day_365: datetime.timedelta(days=365)>, <Period.day_366: datetime.timedelta(days=366)>] + + How are Enums different? ------------------------ @@ -994,6 +1014,9 @@ Supported ``_sunder_`` names - ``_missing_`` -- a lookup function used when a value is not found; may be overridden +- ``_ignore_`` -- a list of names, either as a :func:`list` or a :func:`str`, + that will not be transformed into members, and will be removed from the final + class - ``_order_`` -- used in Python 2/3 code to ensure member order is consistent (class attribute, removed during class creation) - ``_generate_next_value_`` -- used by the `Functional API`_ and by @@ -1001,6 +1024,7 @@ Supported ``_sunder_`` names overridden .. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_`` +.. versionadded:: 3.7 ``_ignore_`` To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute can be provided. It will be checked against the actual order of the enumeration |