blob: b5f9c2f08b1873e202d6bcceca4d5a1c082a413d [file] [log] [blame]
Ethan Furman6b3d64a2013-06-14 16:55:46 -07001:mod:`enum` --- Support for enumerations
2========================================
3
4.. module:: enum
Brett Cannon15e489f2013-06-14 21:59:16 -04005 :synopsis: Implementation of an enumeration class.
6
Andrés Delfino2d748382018-07-07 16:01:25 -03007.. 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 Furman6b3d64a2013-06-14 16:55:46 -070011
R David Murrayfd1ff1c2013-12-20 14:20:49 -050012.. versionadded:: 3.4
13
Ethan Furman6b3d64a2013-06-14 16:55:46 -070014**Source code:** :source:`Lib/enum.py`
15
Ethan Furmanb7751062021-03-30 21:17:26 -070016.. 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 Furman6b3d64a2013-06-14 16:55:46 -070025----------------
26
Ethan Furmanb7751062021-03-30 21:17:26 -070027An enumeration:
Ethan Furmanc72e6382014-02-06 08:13:14 -080028
Ethan Furmanb7751062021-03-30 21:17:26 -070029* 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 Furman542e1df2020-09-14 13:32:44 -070033
Ethan Furmanb7751062021-03-30 21:17:26 -070034Enumerations are created either by using the :keyword:`class` syntax, or by
35using 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
48Even though we can use the :keyword:`class` syntax to create Enums, Enums
49are 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 Furman542e1df2020-09-14 13:32:44 -070060
Ethan Furmanc72e6382014-02-06 08:13:14 -080061
62Module Contents
63---------------
Ethan Furman6b3d64a2013-06-14 16:55:46 -070064
Ethan Furmanb7751062021-03-30 21:17:26 -070065 :class:`EnumType`
Ethan Furmanc72e6382014-02-06 08:13:14 -080066
Ethan Furmanb7751062021-03-30 21:17:26 -070067 The ``type`` for Enum and its subclasses.
Ethan Furmanc72e6382014-02-06 08:13:14 -080068
Ethan Furmanb7751062021-03-30 21:17:26 -070069 :class:`Enum`
Ethan Furmanc72e6382014-02-06 08:13:14 -080070
Ethan Furmanb7751062021-03-30 21:17:26 -070071 Base class for creating enumerated constants.
Ethan Furmanc72e6382014-02-06 08:13:14 -080072
Ethan Furmanb7751062021-03-30 21:17:26 -070073 :class:`IntEnum`
Ethan Furmanc72e6382014-02-06 08:13:14 -080074
Ethan Furmanb7751062021-03-30 21:17:26 -070075 Base class for creating enumerated constants that are also
76 subclasses of :class:`int`.
Ethan Furman0063ff42020-09-21 17:23:13 -070077
Ethan Furmanb7751062021-03-30 21:17:26 -070078 :class:`StrEnum`
Ethan Furman0063ff42020-09-21 17:23:13 -070079
Ethan Furmanb7751062021-03-30 21:17:26 -070080 Base class for creating enumerated constants that are also
81 subclasses of :class:`str`.
Ethan Furman65a5a472016-09-01 23:55:19 -070082
Ethan Furmanb7751062021-03-30 21:17:26 -070083 :class:`Flag`
Ethan Furman65a5a472016-09-01 23:55:19 -070084
Ethan Furmanb7751062021-03-30 21:17:26 -070085 Base class for creating enumerated constants that can be combined using
86 the bitwise operations without losing their :class:`Flag` membership.
Ethan Furman65a5a472016-09-01 23:55:19 -070087
Ethan Furmanb7751062021-03-30 21:17:26 -070088 :class:`IntFlag`
Ethan Furman65a5a472016-09-01 23:55:19 -070089
Ethan Furmanb7751062021-03-30 21:17:26 -070090 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 Furmanc72e6382014-02-06 08:13:14 -080093
Ethan Furmanb7751062021-03-30 21:17:26 -070094 :class:`FlagBoundary`
Ethan Furmanf24bb352013-07-18 17:05:39 -070095
Ethan Furmanb7751062021-03-30 21:17:26 -070096 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 Furmanc16595e2016-09-10 23:36:59 -070099
Ethan Furmanb7751062021-03-30 21:17:26 -0700100 :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 Furmanc16595e2016-09-10 23:36:59 -0700120
121.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
Ethan Furmanefb13be2020-12-10 12:20:06 -0800122.. versionadded:: 3.10 ``StrEnum``
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700123
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700124
Ethan Furmanb7751062021-03-30 21:17:26 -0700125Data Types
126----------
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700127
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700128
Ethan Furmanb7751062021-03-30 21:17:26 -0700129.. class:: EnumType
Ethan Furmanc16595e2016-09-10 23:36:59 -0700130
Ethan Furmanb7751062021-03-30 21:17:26 -0700131 *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 Furmanc16595e2016-09-10 23:36:59 -0700134
Ethan Furmanb7751062021-03-30 21:17:26 -0700135 .. method:: EnumType.__contains__(cls, member)
Ethan Furman455bfde2013-09-08 23:48:34 -0700136
Ethan Furmanb7751062021-03-30 21:17:26 -0700137 Returns ``True`` if member belongs to the ``cls``::
138
139 >>> some_var = Color.RED
140 >>> some_var in Color
141 True
142
Ethan Furman6bd92882021-04-27 13:05:08 -0700143 .. note::
144
145 In Python 3.12 it will be possible to check for member values and not
146 just members; until then, a ``TypeError`` will be raised if a
147 non-Enum-member is used in a containment check.
148
Ethan Furmanb7751062021-03-30 21:17:26 -0700149 .. method:: EnumType.__dir__(cls)
150
151 Returns ``['__class__', '__doc__', '__members__', '__module__']`` and the
152 names of the members in *cls*::
153
154 >>> dir(Color)
155 ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__']
156
157 .. method:: EnumType.__getattr__(cls, name)
158
159 Returns the Enum member in *cls* matching *name*, or raises an :exc:`AttributeError`::
160
161 >>> Color.GREEN
162 Color.GREEN
163
164 .. method:: EnumType.__getitem__(cls, name)
165
166 Returns the Enum member in *cls* matching *name*, or raises an :exc:`KeyError`::
167
168 >>> Color['BLUE']
169 Color.BLUE
170
171 .. method:: EnumType.__iter__(cls)
172
173 Returns each member in *cls* in definition order::
174
175 >>> list(Color)
176 [Color.RED, Color.GREEN, Color.BLUE]
177
178 .. method:: EnumType.__len__(cls)
179
180 Returns the number of member in *cls*::
181
182 >>> len(Color)
183 3
184
185 .. method:: EnumType.__reversed__(cls)
186
187 Returns each member in *cls* in reverse definition order::
188
189 >>> list(reversed(Color))
190 [Color.BLUE, Color.GREEN, Color.RED]
191
192
193.. class:: Enum
194
195 *Enum* is the base class for all *enum* enumerations.
196
197 .. attribute:: Enum.name
198
199 The name used to define the ``Enum`` member::
200
201 >>> Color.BLUE.name
202 'BLUE'
203
204 .. attribute:: Enum.value
205
206 The value given to the ``Enum`` member::
207
208 >>> Color.RED.value
209 1
210
211 .. note:: Enum member values
212
213 Member values can be anything: :class:`int`, :class:`str`, etc.. If
214 the exact value is unimportant you may use :class:`auto` instances and an
215 appropriate value will be chosen for you. Care must be taken if you mix
216 :class:`auto` with other values.
217
218 .. attribute:: Enum._ignore_
219
220 ``_ignore_`` is only used during creation and is removed from the
221 enumeration once that is complete.
222
223 ``_ignore_`` is a list of names that will not become members, and whose
224 names will also be removed from the completed enumeration. See
225 :ref:`TimePeriod <enum-time-period>` for an example.
226
227 .. method:: Enum.__call__(cls, value, names=None, \*, module=None, qualname=None, type=None, start=1, boundary=None)
228
229 This method is called in two different ways:
230
231 * to look up an existing member:
232
233 :cls: The enum class being called.
234 :value: The value to lookup.
235
236 * to use the ``cls`` enum to create a new enum:
237
238 :cls: The enum class being called.
239 :value: The name of the new Enum to create.
240 :names: The names/values of the members for the new Enum.
241 :module: The name of the module the new Enum is created in.
242 :qualname: The actual location in the module where this Enum can be found.
243 :type: A mix-in type for the new Enum.
244 :start: The first integer value for the Enum (used by :class:`auto`)
245 :boundary: How to handle out-of-range values from bit operations (:class:`Flag` only)
246
247 .. method:: Enum.__dir__(self)
248
249 Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and
250 any public methods defined on *self.__class__*::
251
252 >>> from datetime import date
253 >>> class Weekday(Enum):
254 ... MONDAY = 1
255 ... TUESDAY = 2
256 ... WEDNESDAY = 3
257 ... THURSDAY = 4
258 ... FRIDAY = 5
259 ... SATURDAY = 6
260 ... SUNDAY = 7
261 ... @classmethod
262 ... def today(cls):
263 ... print('today is %s' % cls(date.today.isoweekday).naem)
264 >>> dir(Weekday.SATURDAY)
265 ['__class__', '__doc__', '__module__', 'name', 'today', 'value']
266
267 .. method:: Enum._generate_next_value_(name, start, count, last_values)
268
269 :name: The name of the member being defined (e.g. 'RED').
270 :start: The start value for the Enum; the default is 1.
271 :count: The number of members currently defined, not including this one.
272 :last_values: A list of the previous values.
273
274 A *staticmethod* that is used to determine the next value returned by
275 :class:`auto`::
276
277 >>> from enum import auto
278 >>> class PowersOfThree(Enum):
279 ... @staticmethod
280 ... def _generate_next_value_(name, start, count, last_values):
281 ... return (count + 1) * 3
282 ... FIRST = auto()
283 ... SECOND = auto()
284 >>> PowersOfThree.SECOND.value
285 6
286
287 .. method:: Enum._missing_(cls, value)
288
289 A *classmethod* for looking up values not found in *cls*. By default it
290 does nothing, but can be overridden to implement custom search behavior::
291
292 >>> from enum import StrEnum
293 >>> class Build(StrEnum):
294 ... DEBUG = auto()
295 ... OPTIMIZED = auto()
296 ... @classmethod
297 ... def _missing_(cls, value):
298 ... value = value.lower()
299 ... for member in cls:
300 ... if member.value == value:
301 ... return member
302 ... return None
303 >>> Build.DEBUG.value
304 'debug'
305 >>> Build('deBUG')
306 Build.DEBUG
307
308 .. method:: Enum.__repr__(self)
309
310 Returns the string used for *repr()* calls. By default, returns the
311 *Enum* name and the member name, but can be overridden::
312
313 >>> class OldStyle(Enum):
314 ... RETRO = auto()
315 ... OLD_SCHOOl = auto()
316 ... YESTERYEAR = auto()
317 ... def __repr__(self):
318 ... cls_name = self.__class__.__name__
319 ... return f'<{cls_name}.{self.name}: {self.value}>'
320 >>> OldStyle.RETRO
321 <OldStyle.RETRO: 1>
322
323 .. method:: Enum.__str__(self)
324
325 Returns the string used for *str()* calls. By default, returns the
326 member name, but can be overridden::
327
328 >>> class OldStyle(Enum):
329 ... RETRO = auto()
330 ... OLD_SCHOOl = auto()
331 ... YESTERYEAR = auto()
332 ... def __str__(self):
333 ... cls_name = self.__class__.__name__
334 ... return f'{cls_name}.{self.name}'
335 >>> OldStyle.RETRO
336 OldStyle.RETRO
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700337
Ethan Furman9a1daf52013-09-27 22:58:06 -0700338.. note::
339
Ethan Furmanb7751062021-03-30 21:17:26 -0700340 Using :class:`auto` with :class:`Enum` results in integers of increasing value,
341 starting with ``1``.
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700342
343
Ethan Furmanb7751062021-03-30 21:17:26 -0700344.. class:: IntEnum
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700345
Ethan Furmanb7751062021-03-30 21:17:26 -0700346 *IntEnum* is the same as *Enum*, but its members are also integers and can be
347 used anywhere that an integer can be used. If any integer operation is performed
348 with an *IntEnum* member, the resulting value loses its enumeration status.
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700349
Ethan Furmanb7751062021-03-30 21:17:26 -0700350 >>> from enum import IntEnum
351 >>> class Numbers(IntEnum):
352 ... ONE = 1
353 ... TWO = 2
354 ... THREE = 3
355 >>> Numbers.THREE
356 Numbers.THREE
357 >>> Numbers.ONE + Numbers.TWO
358 3
359 >>> Numbers.THREE + 5
360 8
361 >>> Numbers.THREE == 3
362 True
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700363
Ethan Furman101e0742013-09-15 12:34:36 -0700364.. note::
365
Ethan Furmanb7751062021-03-30 21:17:26 -0700366 Using :class:`auto` with :class:`IntEnum` results in integers of increasing value,
367 starting with ``1``.
Ethan Furman101e0742013-09-15 12:34:36 -0700368
Ethan Furmanf24bb352013-07-18 17:05:39 -0700369
Ethan Furmanb7751062021-03-30 21:17:26 -0700370.. class:: StrEnum
Ethan Furmanf24bb352013-07-18 17:05:39 -0700371
Ethan Furmanb7751062021-03-30 21:17:26 -0700372 *StrEnum* is the same as *Enum*, but its members are also strings and can be used
373 in most of the same places that a string can be used. The result of any string
374 operation performed on or with a *StrEnum* member is not part of the enumeration.
375
376 .. note:: There are places in the stdlib that check for an exact :class:`str`
377 instead of a :class:`str` subclass (i.e. ``type(unknown) == str``
378 instead of ``isinstance(str, unknown)``), and in those locations you
379 will need to use ``str(StrEnum.member)``.
380
381
382.. note::
383
384 Using :class:`auto` with :class:`StrEnum` results in values of the member name,
385 lower-cased.
386
387
388.. class:: Flag
389
390 *Flag* members support the bitwise operators ``&`` (*AND*), ``|`` (*OR*),
391 ``^`` (*XOR*), and ``~`` (*INVERT*); the results of those operators are members
392 of the enumeration.
393
394 .. method:: __contains__(self, value)
395
396 Returns *True* if value is in self::
397
398 >>> from enum import Flag, auto
399 >>> class Color(Flag):
400 ... RED = auto()
401 ... GREEN = auto()
402 ... BLUE = auto()
403 >>> purple = Color.RED | Color.BLUE
404 >>> white = Color.RED | Color.GREEN | Color.BLUE
405 >>> Color.GREEN in purple
406 False
407 >>> Color.GREEN in white
408 True
409 >>> purple in white
410 True
411 >>> white in purple
412 False
413
414 .. method:: __iter__(self):
415
416 Returns all contained members::
417
418 >>> list(Color.RED)
419 [Color.RED]
420 >>> list(purple)
421 [Color.RED, Color.BLUE]
422
423 .. method:: __len__(self):
424
425 Returns number of members in flag::
426
427 >>> len(Color.GREEN)
428 1
429 >>> len(white)
430 3
431
432 .. method:: __bool__(self):
433
434 Returns *True* if any members in flag, *False* otherwise::
435
436 >>> bool(Color.GREEN)
437 True
438 >>> bool(white)
439 True
440 >>> black = Color(0)
441 >>> bool(black)
442 False
443
444 .. method:: __or__(self, other)
445
446 Returns current flag binary or'ed with other::
447
448 >>> Color.RED | Color.GREEN
449 Color.RED|Color.GREEN
450
451 .. method:: __and__(self, other)
452
453 Returns current flag binary and'ed with other::
454
455 >>> purple & white
456 Color.RED|Color.BLUE
457 >>> purple & Color.GREEN
458 0x0
459
460 .. method:: __xor__(self, other)
461
462 Returns current flag binary xor'ed with other::
463
464 >>> purple ^ white
465 Color.GREEN
466 >>> purple ^ Color.GREEN
467 Color.RED|Color.GREEN|Color.BLUE
468
469 .. method:: __invert__(self):
470
471 Returns all the flags in *type(self)* that are not in self::
472
473 >>> ~white
474 0x0
475 >>> ~purple
476 Color.GREEN
477 >>> ~Color.RED
478 Color.GREEN|Color.BLUE
479
480.. note::
481
482 Using :class:`auto` with :class:`Flag` results in integers that are powers
483 of two, starting with ``1``.
484
485
486.. class:: IntFlag
487
488 *IntFlag* is the same as *Flag*, but its members are also integers and can be
489 used anywhere that an integer can be used.
490
491 >>> from enum import IntFlag, auto
492 >>> class Color(IntFlag):
493 ... RED = auto()
494 ... GREEN = auto()
495 ... BLUE = auto()
496 >>> Color.RED & 2
497 0x0
498 >>> Color.RED | 2
499 Color.RED|Color.GREEN
500
501 If any integer operation is performed with an *IntFlag* member, the result is
502 not an *IntFlag*::
503
504 >>> Color.RED + 2
505 3
506
507 If a *Flag* operation is performed with an *IntFlag* member and:
508
509 * the result is a valid *IntFlag*: an *IntFlag* is returned
510 * the result is not a valid *IntFlag*: the result depends on the *FlagBoundary* setting
511
512.. note::
513
514 Using :class:`auto` with :class:`IntFlag` results in integers that are powers
515 of two, starting with ``1``.
516
517.. class:: FlagBoundary
518
519 *FlagBoundary* controls how out-of-range values are handled in *Flag* and its
520 subclasses.
521
522 .. attribute:: STRICT
523
524 Out-of-range values cause a :exc:`ValueError` to be raised. This is the
525 default for :class:`Flag`::
526
Ethan Furman49aec1a2021-03-31 09:20:08 -0700527 >>> from enum import Flag, STRICT
Ethan Furmanb7751062021-03-30 21:17:26 -0700528 >>> class StrictFlag(Flag, boundary=STRICT):
529 ... RED = auto()
530 ... GREEN = auto()
531 ... BLUE = auto()
532 >>> StrictFlag(2**2 + 2**4)
533 Traceback (most recent call last):
534 ...
535 ValueError: StrictFlag: invalid value: 20
536 given 0b0 10100
537 allowed 0b0 00111
538
539 .. attribute:: CONFORM
540
541 Out-of-range values have invalid values removed, leaving a valid *Flag*
542 value::
543
Ethan Furman49aec1a2021-03-31 09:20:08 -0700544 >>> from enum import Flag, CONFORM
Ethan Furmanb7751062021-03-30 21:17:26 -0700545 >>> class ConformFlag(Flag, boundary=CONFORM):
546 ... RED = auto()
547 ... GREEN = auto()
548 ... BLUE = auto()
549 >>> ConformFlag(2**2 + 2**4)
550 ConformFlag.BLUE
551
552 .. attribute:: EJECT
553
554 Out-of-range values lose their *Flag* membership and revert to :class:`int`.
555 This is the default for :class:`IntFlag`::
556
Ethan Furman49aec1a2021-03-31 09:20:08 -0700557 >>> from enum import Flag, EJECT
Ethan Furmanb7751062021-03-30 21:17:26 -0700558 >>> class EjectFlag(Flag, boundary=EJECT):
559 ... RED = auto()
560 ... GREEN = auto()
561 ... BLUE = auto()
562 >>> EjectFlag(2**2 + 2**4)
563 20
564
565 .. attribute:: KEEP
566
567 Out-of-range values are kept, and the *Flag* membership is kept. This is
568 used for some stdlib flags:
569
Ethan Furman49aec1a2021-03-31 09:20:08 -0700570 >>> from enum import Flag, KEEP
Ethan Furmanb7751062021-03-30 21:17:26 -0700571 >>> class KeepFlag(Flag, boundary=KEEP):
572 ... RED = auto()
573 ... GREEN = auto()
574 ... BLUE = auto()
575 >>> KeepFlag(2**2 + 2**4)
576 KeepFlag.BLUE|0x10
577
578
579Utilites and Decorators
580-----------------------
581
582.. class:: auto
583
584 *auto* can be used in place of a value. If used, the *Enum* machinery will
585 call an *Enum*'s :meth:`_generate_next_value_` to get an appropriate value.
586 For *Enum* and *IntEnum* that appropriate value will be the last value plus
587 one; for *Flag* and *IntFlag* it will be the first power-of-two greater
588 than the last value; for *StrEnum* it will be the lower-cased version of the
589 member's name.
590
591 ``_generate_next_value_`` can be overridden to customize the values used by
592 *auto*.
593
594.. decorator:: global_enum
595
596 A :keyword:`class` decorator specifically for enumerations. It replaces the
597 :meth:`__repr__` method with one that shows *module_name*.*member_name*. It
598 also injects the members, and their aliases, into the the global namespace
599 they were defined in.
600
601
602.. decorator:: property
603
604 A decorator similar to the built-in *property*, but specifically for
605 enumerations. It allows member attributes to have the same names as members
606 themselves.
607
608 .. note:: the *property* and the member must be defined in separate classes;
609 for example, the *value* and *name* attributes are defined in the
610 *Enum* class, and *Enum* subclasses can define members with the
611 names ``value`` and ``name``.
Ethan Furmanf24bb352013-07-18 17:05:39 -0700612
613.. decorator:: unique
614
Ethan Furmanb7751062021-03-30 21:17:26 -0700615 A :keyword:`class` decorator specifically for enumerations. It searches an
616 enumeration's :attr:`__members__`, gathering any aliases it finds; if any are
617 found :exc:`ValueError` is raised with the details::
Ethan Furmanf24bb352013-07-18 17:05:39 -0700618
Ethan Furmanb7751062021-03-30 21:17:26 -0700619 >>> from enum import Enum, unique
620 >>> @unique
621 ... class Mistake(Enum):
622 ... ONE = 1
623 ... TWO = 2
624 ... THREE = 3
625 ... FOUR = 3
626 ...
627 Traceback (most recent call last):
628 ...
629 ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE