Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1 | :mod:`enum` --- Support for enumerations |
| 2 | ======================================== |
| 3 | |
| 4 | .. module:: enum |
Brett Cannon | 15e489f | 2013-06-14 21:59:16 -0400 | [diff] [blame] | 5 | :synopsis: Implementation of an enumeration class. |
| 6 | |
Andrés Delfino | 2d74838 | 2018-07-07 16:01:25 -0300 | [diff] [blame] | 7 | .. moduleauthor:: Ethan Furman <ethan@stoneleaf.us> |
| 8 | .. sectionauthor:: Barry Warsaw <barry@python.org> |
| 9 | .. sectionauthor:: Eli Bendersky <eliben@gmail.com> |
| 10 | .. sectionauthor:: Ethan Furman <ethan@stoneleaf.us> |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 11 | |
R David Murray | fd1ff1c | 2013-12-20 14:20:49 -0500 | [diff] [blame] | 12 | .. versionadded:: 3.4 |
| 13 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 14 | **Source code:** :source:`Lib/enum.py` |
| 15 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 16 | .. sidebar:: Important |
| 17 | |
| 18 | This page contains the API reference information. For tutorial |
| 19 | information and discussion of more advanced topics, see |
| 20 | |
| 21 | * :ref:`Basic Tutorial <enum-basic-tutorial>` |
| 22 | * :ref:`Advanced Tutorial <enum-advanced-tutorial>` |
| 23 | * :ref:`Enum Cookbook <enum-cookbook>` |
| 24 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 25 | ---------------- |
| 26 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 27 | An enumeration: |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 28 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 29 | * is a set of symbolic names (members) bound to unique values |
| 30 | * can be iterated over to return its members in definition order |
| 31 | * uses :meth:`call` syntax to return members by value |
| 32 | * uses :meth:`index` syntax to return members by name |
Ethan Furman | 542e1df | 2020-09-14 13:32:44 -0700 | [diff] [blame] | 33 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 34 | Enumerations are created either by using the :keyword:`class` syntax, or by |
| 35 | using function-call syntax:: |
| 36 | |
| 37 | >>> from enum import Enum |
| 38 | |
| 39 | >>> # class syntax |
| 40 | >>> class Color(Enum): |
| 41 | ... RED = 1 |
| 42 | ... GREEN = 2 |
| 43 | ... BLUE = 3 |
| 44 | |
| 45 | >>> # functional syntax |
| 46 | >>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE']) |
| 47 | |
| 48 | Even though we can use the :keyword:`class` syntax to create Enums, Enums |
| 49 | are not normal Python classes. See |
| 50 | :ref:`How are Enums different? <enum-class-differences>` for more details. |
| 51 | |
| 52 | .. note:: Nomenclature |
| 53 | |
| 54 | - The class :class:`Color` is an *enumeration* (or *enum*) |
| 55 | - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are |
| 56 | *enumeration members* (or *enum members*) and are functionally constants. |
| 57 | - The enum members have *names* and *values* (the name of |
| 58 | :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is |
| 59 | ``3``, etc.) |
Ethan Furman | 542e1df | 2020-09-14 13:32:44 -0700 | [diff] [blame] | 60 | |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 61 | |
| 62 | Module Contents |
| 63 | --------------- |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 64 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 65 | :class:`EnumType` |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 66 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 67 | The ``type`` for Enum and its subclasses. |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 68 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 69 | :class:`Enum` |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 70 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 71 | Base class for creating enumerated constants. |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 72 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 73 | :class:`IntEnum` |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 74 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 75 | Base class for creating enumerated constants that are also |
| 76 | subclasses of :class:`int`. |
Ethan Furman | 0063ff4 | 2020-09-21 17:23:13 -0700 | [diff] [blame] | 77 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 78 | :class:`StrEnum` |
Ethan Furman | 0063ff4 | 2020-09-21 17:23:13 -0700 | [diff] [blame] | 79 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 80 | Base class for creating enumerated constants that are also |
| 81 | subclasses of :class:`str`. |
Ethan Furman | 65a5a47 | 2016-09-01 23:55:19 -0700 | [diff] [blame] | 82 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 83 | :class:`Flag` |
Ethan Furman | 65a5a47 | 2016-09-01 23:55:19 -0700 | [diff] [blame] | 84 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 85 | Base class for creating enumerated constants that can be combined using |
| 86 | the bitwise operations without losing their :class:`Flag` membership. |
Ethan Furman | 65a5a47 | 2016-09-01 23:55:19 -0700 | [diff] [blame] | 87 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 88 | :class:`IntFlag` |
Ethan Furman | 65a5a47 | 2016-09-01 23:55:19 -0700 | [diff] [blame] | 89 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 90 | Base class for creating enumerated constants that can be combined using |
| 91 | the bitwise operators without losing their :class:`IntFlag` membership. |
| 92 | :class:`IntFlag` members are also subclasses of :class:`int`. |
Ethan Furman | c72e638 | 2014-02-06 08:13:14 -0800 | [diff] [blame] | 93 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 94 | :class:`FlagBoundary` |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 95 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 96 | An enumeration with the values ``STRICT``, ``CONFORM``, ``EJECT``, and |
| 97 | ``KEEP`` which allows for more fine-grained control over how invalid values |
| 98 | are dealt with in an enumeration. |
Ethan Furman | c16595e | 2016-09-10 23:36:59 -0700 | [diff] [blame] | 99 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 100 | :class:`auto` |
| 101 | |
| 102 | Instances are replaced with an appropriate value for Enum members. |
| 103 | :class:`StrEnum` defaults to the lower-cased version of the member name, |
| 104 | while other Enums default to 1 and increase from there. |
| 105 | |
| 106 | :func:`global_enum` |
| 107 | |
| 108 | :class:`Enum` class decorator to apply the appropriate global `__repr__`, |
| 109 | and export its members into the global name space. |
| 110 | |
| 111 | :func:`property` |
| 112 | |
| 113 | Allows :class:`Enum` members to have attributes without conflicting with |
| 114 | other members' names. |
| 115 | |
| 116 | :func:`unique` |
| 117 | |
| 118 | Enum class decorator that ensures only one name is bound to any one value. |
| 119 | |
Ethan Furman | c16595e | 2016-09-10 23:36:59 -0700 | [diff] [blame] | 120 | |
| 121 | .. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto`` |
Ethan Furman | efb13be | 2020-12-10 12:20:06 -0800 | [diff] [blame] | 122 | .. versionadded:: 3.10 ``StrEnum`` |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 123 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 124 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 125 | Data Types |
| 126 | ---------- |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 127 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 128 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 129 | .. class:: EnumType |
Ethan Furman | c16595e | 2016-09-10 23:36:59 -0700 | [diff] [blame] | 130 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 131 | *EnumType* is the :term:`metaclass` for *enum* enumerations. It is possible |
| 132 | to subclass *EnumType* -- see :ref:`Subclassing EnumType <enumtype-examples>` |
| 133 | for details. |
Ethan Furman | c16595e | 2016-09-10 23:36:59 -0700 | [diff] [blame] | 134 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 135 | .. method:: EnumType.__contains__(cls, member) |
Ethan Furman | 455bfde | 2013-09-08 23:48:34 -0700 | [diff] [blame] | 136 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 137 | Returns ``True`` if member belongs to the ``cls``:: |
| 138 | |
| 139 | >>> some_var = Color.RED |
| 140 | >>> some_var in Color |
| 141 | True |
| 142 | |
| 143 | .. method:: EnumType.__dir__(cls) |
| 144 | |
| 145 | Returns ``['__class__', '__doc__', '__members__', '__module__']`` and the |
| 146 | names of the members in *cls*:: |
| 147 | |
| 148 | >>> dir(Color) |
| 149 | ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__'] |
| 150 | |
| 151 | .. method:: EnumType.__getattr__(cls, name) |
| 152 | |
| 153 | Returns the Enum member in *cls* matching *name*, or raises an :exc:`AttributeError`:: |
| 154 | |
| 155 | >>> Color.GREEN |
| 156 | Color.GREEN |
| 157 | |
| 158 | .. method:: EnumType.__getitem__(cls, name) |
| 159 | |
| 160 | Returns the Enum member in *cls* matching *name*, or raises an :exc:`KeyError`:: |
| 161 | |
| 162 | >>> Color['BLUE'] |
| 163 | Color.BLUE |
| 164 | |
| 165 | .. method:: EnumType.__iter__(cls) |
| 166 | |
| 167 | Returns each member in *cls* in definition order:: |
| 168 | |
| 169 | >>> list(Color) |
| 170 | [Color.RED, Color.GREEN, Color.BLUE] |
| 171 | |
| 172 | .. method:: EnumType.__len__(cls) |
| 173 | |
| 174 | Returns the number of member in *cls*:: |
| 175 | |
| 176 | >>> len(Color) |
| 177 | 3 |
| 178 | |
| 179 | .. method:: EnumType.__reversed__(cls) |
| 180 | |
| 181 | Returns each member in *cls* in reverse definition order:: |
| 182 | |
| 183 | >>> list(reversed(Color)) |
| 184 | [Color.BLUE, Color.GREEN, Color.RED] |
| 185 | |
| 186 | |
| 187 | .. class:: Enum |
| 188 | |
| 189 | *Enum* is the base class for all *enum* enumerations. |
| 190 | |
| 191 | .. attribute:: Enum.name |
| 192 | |
| 193 | The name used to define the ``Enum`` member:: |
| 194 | |
| 195 | >>> Color.BLUE.name |
| 196 | 'BLUE' |
| 197 | |
| 198 | .. attribute:: Enum.value |
| 199 | |
| 200 | The value given to the ``Enum`` member:: |
| 201 | |
| 202 | >>> Color.RED.value |
| 203 | 1 |
| 204 | |
| 205 | .. note:: Enum member values |
| 206 | |
| 207 | Member values can be anything: :class:`int`, :class:`str`, etc.. If |
| 208 | the exact value is unimportant you may use :class:`auto` instances and an |
| 209 | appropriate value will be chosen for you. Care must be taken if you mix |
| 210 | :class:`auto` with other values. |
| 211 | |
| 212 | .. attribute:: Enum._ignore_ |
| 213 | |
| 214 | ``_ignore_`` is only used during creation and is removed from the |
| 215 | enumeration once that is complete. |
| 216 | |
| 217 | ``_ignore_`` is a list of names that will not become members, and whose |
| 218 | names will also be removed from the completed enumeration. See |
| 219 | :ref:`TimePeriod <enum-time-period>` for an example. |
| 220 | |
| 221 | .. method:: Enum.__call__(cls, value, names=None, \*, module=None, qualname=None, type=None, start=1, boundary=None) |
| 222 | |
| 223 | This method is called in two different ways: |
| 224 | |
| 225 | * to look up an existing member: |
| 226 | |
| 227 | :cls: The enum class being called. |
| 228 | :value: The value to lookup. |
| 229 | |
| 230 | * to use the ``cls`` enum to create a new enum: |
| 231 | |
| 232 | :cls: The enum class being called. |
| 233 | :value: The name of the new Enum to create. |
| 234 | :names: The names/values of the members for the new Enum. |
| 235 | :module: The name of the module the new Enum is created in. |
| 236 | :qualname: The actual location in the module where this Enum can be found. |
| 237 | :type: A mix-in type for the new Enum. |
| 238 | :start: The first integer value for the Enum (used by :class:`auto`) |
| 239 | :boundary: How to handle out-of-range values from bit operations (:class:`Flag` only) |
| 240 | |
| 241 | .. method:: Enum.__dir__(self) |
| 242 | |
| 243 | Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and |
| 244 | any public methods defined on *self.__class__*:: |
| 245 | |
| 246 | >>> from datetime import date |
| 247 | >>> class Weekday(Enum): |
| 248 | ... MONDAY = 1 |
| 249 | ... TUESDAY = 2 |
| 250 | ... WEDNESDAY = 3 |
| 251 | ... THURSDAY = 4 |
| 252 | ... FRIDAY = 5 |
| 253 | ... SATURDAY = 6 |
| 254 | ... SUNDAY = 7 |
| 255 | ... @classmethod |
| 256 | ... def today(cls): |
| 257 | ... print('today is %s' % cls(date.today.isoweekday).naem) |
| 258 | >>> dir(Weekday.SATURDAY) |
| 259 | ['__class__', '__doc__', '__module__', 'name', 'today', 'value'] |
| 260 | |
| 261 | .. method:: Enum._generate_next_value_(name, start, count, last_values) |
| 262 | |
| 263 | :name: The name of the member being defined (e.g. 'RED'). |
| 264 | :start: The start value for the Enum; the default is 1. |
| 265 | :count: The number of members currently defined, not including this one. |
| 266 | :last_values: A list of the previous values. |
| 267 | |
| 268 | A *staticmethod* that is used to determine the next value returned by |
| 269 | :class:`auto`:: |
| 270 | |
| 271 | >>> from enum import auto |
| 272 | >>> class PowersOfThree(Enum): |
| 273 | ... @staticmethod |
| 274 | ... def _generate_next_value_(name, start, count, last_values): |
| 275 | ... return (count + 1) * 3 |
| 276 | ... FIRST = auto() |
| 277 | ... SECOND = auto() |
| 278 | >>> PowersOfThree.SECOND.value |
| 279 | 6 |
| 280 | |
| 281 | .. method:: Enum._missing_(cls, value) |
| 282 | |
| 283 | A *classmethod* for looking up values not found in *cls*. By default it |
| 284 | does nothing, but can be overridden to implement custom search behavior:: |
| 285 | |
| 286 | >>> from enum import StrEnum |
| 287 | >>> class Build(StrEnum): |
| 288 | ... DEBUG = auto() |
| 289 | ... OPTIMIZED = auto() |
| 290 | ... @classmethod |
| 291 | ... def _missing_(cls, value): |
| 292 | ... value = value.lower() |
| 293 | ... for member in cls: |
| 294 | ... if member.value == value: |
| 295 | ... return member |
| 296 | ... return None |
| 297 | >>> Build.DEBUG.value |
| 298 | 'debug' |
| 299 | >>> Build('deBUG') |
| 300 | Build.DEBUG |
| 301 | |
| 302 | .. method:: Enum.__repr__(self) |
| 303 | |
| 304 | Returns the string used for *repr()* calls. By default, returns the |
| 305 | *Enum* name and the member name, but can be overridden:: |
| 306 | |
| 307 | >>> class OldStyle(Enum): |
| 308 | ... RETRO = auto() |
| 309 | ... OLD_SCHOOl = auto() |
| 310 | ... YESTERYEAR = auto() |
| 311 | ... def __repr__(self): |
| 312 | ... cls_name = self.__class__.__name__ |
| 313 | ... return f'<{cls_name}.{self.name}: {self.value}>' |
| 314 | >>> OldStyle.RETRO |
| 315 | <OldStyle.RETRO: 1> |
| 316 | |
| 317 | .. method:: Enum.__str__(self) |
| 318 | |
| 319 | Returns the string used for *str()* calls. By default, returns the |
| 320 | member name, but can be overridden:: |
| 321 | |
| 322 | >>> class OldStyle(Enum): |
| 323 | ... RETRO = auto() |
| 324 | ... OLD_SCHOOl = auto() |
| 325 | ... YESTERYEAR = auto() |
| 326 | ... def __str__(self): |
| 327 | ... cls_name = self.__class__.__name__ |
| 328 | ... return f'{cls_name}.{self.name}' |
| 329 | >>> OldStyle.RETRO |
| 330 | OldStyle.RETRO |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 331 | |
Ethan Furman | 9a1daf5 | 2013-09-27 22:58:06 -0700 | [diff] [blame] | 332 | .. note:: |
| 333 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 334 | Using :class:`auto` with :class:`Enum` results in integers of increasing value, |
| 335 | starting with ``1``. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 336 | |
| 337 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 338 | .. class:: IntEnum |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 339 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 340 | *IntEnum* is the same as *Enum*, but its members are also integers and can be |
| 341 | used anywhere that an integer can be used. If any integer operation is performed |
| 342 | with an *IntEnum* member, the resulting value loses its enumeration status. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 343 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 344 | >>> from enum import IntEnum |
| 345 | >>> class Numbers(IntEnum): |
| 346 | ... ONE = 1 |
| 347 | ... TWO = 2 |
| 348 | ... THREE = 3 |
| 349 | >>> Numbers.THREE |
| 350 | Numbers.THREE |
| 351 | >>> Numbers.ONE + Numbers.TWO |
| 352 | 3 |
| 353 | >>> Numbers.THREE + 5 |
| 354 | 8 |
| 355 | >>> Numbers.THREE == 3 |
| 356 | True |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 357 | |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 358 | .. note:: |
| 359 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 360 | Using :class:`auto` with :class:`IntEnum` results in integers of increasing value, |
| 361 | starting with ``1``. |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 362 | |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 363 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 364 | .. class:: StrEnum |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 365 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 366 | *StrEnum* is the same as *Enum*, but its members are also strings and can be used |
| 367 | in most of the same places that a string can be used. The result of any string |
| 368 | operation performed on or with a *StrEnum* member is not part of the enumeration. |
| 369 | |
| 370 | .. note:: There are places in the stdlib that check for an exact :class:`str` |
| 371 | instead of a :class:`str` subclass (i.e. ``type(unknown) == str`` |
| 372 | instead of ``isinstance(str, unknown)``), and in those locations you |
| 373 | will need to use ``str(StrEnum.member)``. |
| 374 | |
| 375 | |
| 376 | .. note:: |
| 377 | |
| 378 | Using :class:`auto` with :class:`StrEnum` results in values of the member name, |
| 379 | lower-cased. |
| 380 | |
| 381 | |
| 382 | .. class:: Flag |
| 383 | |
| 384 | *Flag* members support the bitwise operators ``&`` (*AND*), ``|`` (*OR*), |
| 385 | ``^`` (*XOR*), and ``~`` (*INVERT*); the results of those operators are members |
| 386 | of the enumeration. |
| 387 | |
| 388 | .. method:: __contains__(self, value) |
| 389 | |
| 390 | Returns *True* if value is in self:: |
| 391 | |
| 392 | >>> from enum import Flag, auto |
| 393 | >>> class Color(Flag): |
| 394 | ... RED = auto() |
| 395 | ... GREEN = auto() |
| 396 | ... BLUE = auto() |
| 397 | >>> purple = Color.RED | Color.BLUE |
| 398 | >>> white = Color.RED | Color.GREEN | Color.BLUE |
| 399 | >>> Color.GREEN in purple |
| 400 | False |
| 401 | >>> Color.GREEN in white |
| 402 | True |
| 403 | >>> purple in white |
| 404 | True |
| 405 | >>> white in purple |
| 406 | False |
| 407 | |
| 408 | .. method:: __iter__(self): |
| 409 | |
| 410 | Returns all contained members:: |
| 411 | |
| 412 | >>> list(Color.RED) |
| 413 | [Color.RED] |
| 414 | >>> list(purple) |
| 415 | [Color.RED, Color.BLUE] |
| 416 | |
| 417 | .. method:: __len__(self): |
| 418 | |
| 419 | Returns number of members in flag:: |
| 420 | |
| 421 | >>> len(Color.GREEN) |
| 422 | 1 |
| 423 | >>> len(white) |
| 424 | 3 |
| 425 | |
| 426 | .. method:: __bool__(self): |
| 427 | |
| 428 | Returns *True* if any members in flag, *False* otherwise:: |
| 429 | |
| 430 | >>> bool(Color.GREEN) |
| 431 | True |
| 432 | >>> bool(white) |
| 433 | True |
| 434 | >>> black = Color(0) |
| 435 | >>> bool(black) |
| 436 | False |
| 437 | |
| 438 | .. method:: __or__(self, other) |
| 439 | |
| 440 | Returns current flag binary or'ed with other:: |
| 441 | |
| 442 | >>> Color.RED | Color.GREEN |
| 443 | Color.RED|Color.GREEN |
| 444 | |
| 445 | .. method:: __and__(self, other) |
| 446 | |
| 447 | Returns current flag binary and'ed with other:: |
| 448 | |
| 449 | >>> purple & white |
| 450 | Color.RED|Color.BLUE |
| 451 | >>> purple & Color.GREEN |
| 452 | 0x0 |
| 453 | |
| 454 | .. method:: __xor__(self, other) |
| 455 | |
| 456 | Returns current flag binary xor'ed with other:: |
| 457 | |
| 458 | >>> purple ^ white |
| 459 | Color.GREEN |
| 460 | >>> purple ^ Color.GREEN |
| 461 | Color.RED|Color.GREEN|Color.BLUE |
| 462 | |
| 463 | .. method:: __invert__(self): |
| 464 | |
| 465 | Returns all the flags in *type(self)* that are not in self:: |
| 466 | |
| 467 | >>> ~white |
| 468 | 0x0 |
| 469 | >>> ~purple |
| 470 | Color.GREEN |
| 471 | >>> ~Color.RED |
| 472 | Color.GREEN|Color.BLUE |
| 473 | |
| 474 | .. note:: |
| 475 | |
| 476 | Using :class:`auto` with :class:`Flag` results in integers that are powers |
| 477 | of two, starting with ``1``. |
| 478 | |
| 479 | |
| 480 | .. class:: IntFlag |
| 481 | |
| 482 | *IntFlag* is the same as *Flag*, but its members are also integers and can be |
| 483 | used anywhere that an integer can be used. |
| 484 | |
| 485 | >>> from enum import IntFlag, auto |
| 486 | >>> class Color(IntFlag): |
| 487 | ... RED = auto() |
| 488 | ... GREEN = auto() |
| 489 | ... BLUE = auto() |
| 490 | >>> Color.RED & 2 |
| 491 | 0x0 |
| 492 | >>> Color.RED | 2 |
| 493 | Color.RED|Color.GREEN |
| 494 | |
| 495 | If any integer operation is performed with an *IntFlag* member, the result is |
| 496 | not an *IntFlag*:: |
| 497 | |
| 498 | >>> Color.RED + 2 |
| 499 | 3 |
| 500 | |
| 501 | If a *Flag* operation is performed with an *IntFlag* member and: |
| 502 | |
| 503 | * the result is a valid *IntFlag*: an *IntFlag* is returned |
| 504 | * the result is not a valid *IntFlag*: the result depends on the *FlagBoundary* setting |
| 505 | |
| 506 | .. note:: |
| 507 | |
| 508 | Using :class:`auto` with :class:`IntFlag` results in integers that are powers |
| 509 | of two, starting with ``1``. |
| 510 | |
| 511 | .. class:: FlagBoundary |
| 512 | |
| 513 | *FlagBoundary* controls how out-of-range values are handled in *Flag* and its |
| 514 | subclasses. |
| 515 | |
| 516 | .. attribute:: STRICT |
| 517 | |
| 518 | Out-of-range values cause a :exc:`ValueError` to be raised. This is the |
| 519 | default for :class:`Flag`:: |
| 520 | |
| 521 | >>> from enum import STRICT |
| 522 | >>> class StrictFlag(Flag, boundary=STRICT): |
| 523 | ... RED = auto() |
| 524 | ... GREEN = auto() |
| 525 | ... BLUE = auto() |
| 526 | >>> StrictFlag(2**2 + 2**4) |
| 527 | Traceback (most recent call last): |
| 528 | ... |
| 529 | ValueError: StrictFlag: invalid value: 20 |
| 530 | given 0b0 10100 |
| 531 | allowed 0b0 00111 |
| 532 | |
| 533 | .. attribute:: CONFORM |
| 534 | |
| 535 | Out-of-range values have invalid values removed, leaving a valid *Flag* |
| 536 | value:: |
| 537 | |
| 538 | >>> from enum import CONFORM |
| 539 | >>> class ConformFlag(Flag, boundary=CONFORM): |
| 540 | ... RED = auto() |
| 541 | ... GREEN = auto() |
| 542 | ... BLUE = auto() |
| 543 | >>> ConformFlag(2**2 + 2**4) |
| 544 | ConformFlag.BLUE |
| 545 | |
| 546 | .. attribute:: EJECT |
| 547 | |
| 548 | Out-of-range values lose their *Flag* membership and revert to :class:`int`. |
| 549 | This is the default for :class:`IntFlag`:: |
| 550 | |
| 551 | >>> from enum import EJECT |
| 552 | >>> class EjectFlag(Flag, boundary=EJECT): |
| 553 | ... RED = auto() |
| 554 | ... GREEN = auto() |
| 555 | ... BLUE = auto() |
| 556 | >>> EjectFlag(2**2 + 2**4) |
| 557 | 20 |
| 558 | |
| 559 | .. attribute:: KEEP |
| 560 | |
| 561 | Out-of-range values are kept, and the *Flag* membership is kept. This is |
| 562 | used for some stdlib flags: |
| 563 | |
| 564 | >>> from enum import KEEP |
| 565 | >>> class KeepFlag(Flag, boundary=KEEP): |
| 566 | ... RED = auto() |
| 567 | ... GREEN = auto() |
| 568 | ... BLUE = auto() |
| 569 | >>> KeepFlag(2**2 + 2**4) |
| 570 | KeepFlag.BLUE|0x10 |
| 571 | |
| 572 | |
| 573 | Utilites and Decorators |
| 574 | ----------------------- |
| 575 | |
| 576 | .. class:: auto |
| 577 | |
| 578 | *auto* can be used in place of a value. If used, the *Enum* machinery will |
| 579 | call an *Enum*'s :meth:`_generate_next_value_` to get an appropriate value. |
| 580 | For *Enum* and *IntEnum* that appropriate value will be the last value plus |
| 581 | one; for *Flag* and *IntFlag* it will be the first power-of-two greater |
| 582 | than the last value; for *StrEnum* it will be the lower-cased version of the |
| 583 | member's name. |
| 584 | |
| 585 | ``_generate_next_value_`` can be overridden to customize the values used by |
| 586 | *auto*. |
| 587 | |
| 588 | .. decorator:: global_enum |
| 589 | |
| 590 | A :keyword:`class` decorator specifically for enumerations. It replaces the |
| 591 | :meth:`__repr__` method with one that shows *module_name*.*member_name*. It |
| 592 | also injects the members, and their aliases, into the the global namespace |
| 593 | they were defined in. |
| 594 | |
| 595 | |
| 596 | .. decorator:: property |
| 597 | |
| 598 | A decorator similar to the built-in *property*, but specifically for |
| 599 | enumerations. It allows member attributes to have the same names as members |
| 600 | themselves. |
| 601 | |
| 602 | .. note:: the *property* and the member must be defined in separate classes; |
| 603 | for example, the *value* and *name* attributes are defined in the |
| 604 | *Enum* class, and *Enum* subclasses can define members with the |
| 605 | names ``value`` and ``name``. |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 606 | |
| 607 | .. decorator:: unique |
| 608 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 609 | A :keyword:`class` decorator specifically for enumerations. It searches an |
| 610 | enumeration's :attr:`__members__`, gathering any aliases it finds; if any are |
| 611 | found :exc:`ValueError` is raised with the details:: |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 612 | |
Ethan Furman | b775106 | 2021-03-30 21:17:26 -0700 | [diff] [blame^] | 613 | >>> from enum import Enum, unique |
| 614 | >>> @unique |
| 615 | ... class Mistake(Enum): |
| 616 | ... ONE = 1 |
| 617 | ... TWO = 2 |
| 618 | ... THREE = 3 |
| 619 | ... FOUR = 3 |
| 620 | ... |
| 621 | Traceback (most recent call last): |
| 622 | ... |
| 623 | ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE |
Ethan Furman | 7aaeb2a | 2021-01-25 14:26:19 -0800 | [diff] [blame] | 624 | |