blob: d3b838c01c7ed34552d73d2e11a7050d5eaa498f [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
Ethan Furman6b3d64a2013-06-14 16:55:46 -07007.. :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>
11
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
16----------------
17
Ethan Furmanc72e6382014-02-06 08:13:14 -080018An enumeration is a set of symbolic names (members) bound to unique,
19constant values. Within an enumeration, the members can be compared
20by identity, and the enumeration itself can be iterated over.
21
22
23Module Contents
24---------------
Ethan Furman6b3d64a2013-06-14 16:55:46 -070025
26This module defines two enumeration classes that can be used to define unique
Ethan Furmanf24bb352013-07-18 17:05:39 -070027sets of names and values: :class:`Enum` and :class:`IntEnum`. It also defines
Ethan Furmanc72e6382014-02-06 08:13:14 -080028one decorator, :func:`unique`.
29
30.. class:: Enum
31
32 Base class for creating enumerated constants. See section
Larry Hastingsad88d7a2014-02-10 04:26:10 -080033 `Functional API`_ for an alternate construction syntax.
Ethan Furmanc72e6382014-02-06 08:13:14 -080034
35.. class:: IntEnum
36
37 Base class for creating enumerated constants that are also
38 subclasses of :class:`int`.
39
40.. function:: unique
41
42 Enum class decorator that ensures only one name is bound to any one value.
Ethan Furmanf24bb352013-07-18 17:05:39 -070043
Ethan Furman6b3d64a2013-06-14 16:55:46 -070044
45Creating an Enum
46----------------
47
48Enumerations are created using the :keyword:`class` syntax, which makes them
49easy to read and write. An alternative creation method is described in
50`Functional API`_. To define an enumeration, subclass :class:`Enum` as
51follows::
52
53 >>> from enum import Enum
54 >>> class Color(Enum):
55 ... red = 1
56 ... green = 2
57 ... blue = 3
Ethan Furmaned0bf8a2013-09-06 19:53:30 -070058 ...
Ethan Furman6b3d64a2013-06-14 16:55:46 -070059
Ethan Furman455bfde2013-09-08 23:48:34 -070060.. note:: Nomenclature
61
Ethan Furmaned0bf8a2013-09-06 19:53:30 -070062 - The class :class:`Color` is an *enumeration* (or *enum*)
63 - The attributes :attr:`Color.red`, :attr:`Color.green`, etc., are
64 *enumeration members* (or *enum members*).
65 - The enum members have *names* and *values* (the name of
66 :attr:`Color.red` is ``red``, the value of :attr:`Color.blue` is
67 ``3``, etc.)
Ethan Furman6b3d64a2013-06-14 16:55:46 -070068
Ethan Furman9a1daf52013-09-27 22:58:06 -070069.. note::
70
71 Even though we use the :keyword:`class` syntax to create Enums, Enums
72 are not normal Python classes. See `How are Enums different?`_ for
73 more details.
74
Ethan Furman6b3d64a2013-06-14 16:55:46 -070075Enumeration members have human readable string representations::
76
77 >>> print(Color.red)
78 Color.red
79
80...while their ``repr`` has more information::
81
82 >>> print(repr(Color.red))
83 <Color.red: 1>
84
85The *type* of an enumeration member is the enumeration it belongs to::
86
87 >>> type(Color.red)
88 <enum 'Color'>
89 >>> isinstance(Color.green, Color)
90 True
91 >>>
92
93Enum members also have a property that contains just their item name::
94
95 >>> print(Color.red.name)
96 red
97
98Enumerations support iteration, in definition order::
99
100 >>> class Shake(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700101 ... vanilla = 7
102 ... chocolate = 4
103 ... cookies = 9
104 ... mint = 3
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700105 ...
106 >>> for shake in Shake:
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700107 ... print(shake)
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700108 ...
109 Shake.vanilla
110 Shake.chocolate
111 Shake.cookies
112 Shake.mint
113
114Enumeration members are hashable, so they can be used in dictionaries and sets::
115
116 >>> apples = {}
117 >>> apples[Color.red] = 'red delicious'
118 >>> apples[Color.green] = 'granny smith'
119 >>> apples == {Color.red: 'red delicious', Color.green: 'granny smith'}
120 True
121
122
Ethan Furman3fe70b4a2013-06-28 14:02:34 -0700123Programmatic access to enumeration members and their attributes
124---------------------------------------------------------------
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700125
126Sometimes it's useful to access members in enumerations programmatically (i.e.
127situations where ``Color.red`` won't do because the exact color is not known
128at program-writing time). ``Enum`` allows such access::
129
130 >>> Color(1)
131 <Color.red: 1>
132 >>> Color(3)
133 <Color.blue: 3>
134
135If you want to access enum members by *name*, use item access::
136
137 >>> Color['red']
138 <Color.red: 1>
139 >>> Color['green']
140 <Color.green: 2>
141
Larry Hastings3732ed22014-03-15 21:13:56 -0700142If you have an enum member and need its :attr:`name` or :attr:`value`::
Ethan Furman3fe70b4a2013-06-28 14:02:34 -0700143
144 >>> member = Color.red
145 >>> member.name
146 'red'
147 >>> member.value
148 1
149
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700150
151Duplicating enum members and values
152-----------------------------------
153
154Having two enum members with the same name is invalid::
155
156 >>> class Shape(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700157 ... square = 2
158 ... square = 3
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700159 ...
160 Traceback (most recent call last):
161 ...
162 TypeError: Attempted to reuse key: 'square'
163
164However, two enum members are allowed to have the same value. Given two members
165A and B with the same value (and A defined first), B is an alias to A. By-value
166lookup of the value of A and B will return A. By-name lookup of B will also
167return A::
168
169 >>> class Shape(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700170 ... square = 2
171 ... diamond = 1
172 ... circle = 3
173 ... alias_for_square = 2
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700174 ...
175 >>> Shape.square
176 <Shape.square: 2>
177 >>> Shape.alias_for_square
178 <Shape.square: 2>
179 >>> Shape(2)
180 <Shape.square: 2>
181
Ethan Furman101e0742013-09-15 12:34:36 -0700182.. note::
183
184 Attempting to create a member with the same name as an already
185 defined attribute (another member, a method, etc.) or attempting to create
186 an attribute with the same name as a member is not allowed.
187
Ethan Furmanf24bb352013-07-18 17:05:39 -0700188
189Ensuring unique enumeration values
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700190----------------------------------
Ethan Furmanf24bb352013-07-18 17:05:39 -0700191
192By default, enumerations allow multiple names as aliases for the same value.
193When this behavior isn't desired, the following decorator can be used to
194ensure each value is used only once in the enumeration:
195
196.. decorator:: unique
197
198A :keyword:`class` decorator specifically for enumerations. It searches an
199enumeration's :attr:`__members__` gathering any aliases it finds; if any are
200found :exc:`ValueError` is raised with the details::
201
202 >>> from enum import Enum, unique
203 >>> @unique
204 ... class Mistake(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700205 ... one = 1
206 ... two = 2
207 ... three = 3
208 ... four = 3
209 ...
Ethan Furmanf24bb352013-07-18 17:05:39 -0700210 Traceback (most recent call last):
211 ...
212 ValueError: duplicate values found in <enum 'Mistake'>: four -> three
213
214
215Iteration
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700216---------
Ethan Furmanf24bb352013-07-18 17:05:39 -0700217
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700218Iterating over the members of an enum does not provide the aliases::
219
220 >>> list(Shape)
221 [<Shape.square: 2>, <Shape.diamond: 1>, <Shape.circle: 3>]
222
223The special attribute ``__members__`` is an ordered dictionary mapping names
224to members. It includes all names defined in the enumeration, including the
225aliases::
226
227 >>> for name, member in Shape.__members__.items():
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700228 ... name, member
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700229 ...
230 ('square', <Shape.square: 2>)
231 ('diamond', <Shape.diamond: 1>)
232 ('circle', <Shape.circle: 3>)
233 ('alias_for_square', <Shape.square: 2>)
234
235The ``__members__`` attribute can be used for detailed programmatic access to
236the enumeration members. For example, finding all the aliases::
237
238 >>> [name for name, member in Shape.__members__.items() if member.name != name]
239 ['alias_for_square']
240
Ethan Furmanf24bb352013-07-18 17:05:39 -0700241
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700242Comparisons
243-----------
244
245Enumeration members are compared by identity::
246
247 >>> Color.red is Color.red
248 True
249 >>> Color.red is Color.blue
250 False
251 >>> Color.red is not Color.blue
252 True
253
254Ordered comparisons between enumeration values are *not* supported. Enum
255members are not integers (but see `IntEnum`_ below)::
256
257 >>> Color.red < Color.blue
258 Traceback (most recent call last):
259 File "<stdin>", line 1, in <module>
260 TypeError: unorderable types: Color() < Color()
261
262Equality comparisons are defined though::
263
264 >>> Color.blue == Color.red
265 False
266 >>> Color.blue != Color.red
267 True
268 >>> Color.blue == Color.blue
269 True
270
271Comparisons against non-enumeration values will always compare not equal
Ezio Melotti93d7dda2013-10-05 04:13:18 +0300272(again, :class:`IntEnum` was explicitly designed to behave differently, see
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700273below)::
274
275 >>> Color.blue == 2
276 False
277
278
279Allowed members and attributes of enumerations
280----------------------------------------------
281
282The examples above use integers for enumeration values. Using integers is
283short and handy (and provided by default by the `Functional API`_), but not
284strictly enforced. In the vast majority of use-cases, one doesn't care what
285the actual value of an enumeration is. But if the value *is* important,
286enumerations can have arbitrary values.
287
288Enumerations are Python classes, and can have methods and special methods as
289usual. If we have this enumeration::
290
291 >>> class Mood(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700292 ... funky = 1
293 ... happy = 3
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700294 ...
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700295 ... def describe(self):
296 ... # self is the member here
297 ... return self.name, self.value
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700298 ...
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700299 ... def __str__(self):
300 ... return 'my custom str! {0}'.format(self.value)
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700301 ...
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700302 ... @classmethod
303 ... def favorite_mood(cls):
304 ... # cls here is the enumeration
305 ... return cls.happy
306 ...
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700307
308Then::
309
310 >>> Mood.favorite_mood()
311 <Mood.happy: 3>
312 >>> Mood.happy.describe()
313 ('happy', 3)
314 >>> str(Mood.funky)
315 'my custom str! 1'
316
Ethan Furman8be6fac2014-11-01 07:40:22 -0700317The rules for what is allowed are as follows: names that start and end with a
318with a single underscore are reserved by enum and cannot be used; all other
319attributes defined within an enumeration will become members of this
320enumeration, with the exception of special methods (:meth:`__str__`,
321:meth:`__add__`, etc.) and descriptors (methods are also descriptors).
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700322
323Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then
324whatever value(s) were given to the enum member will be passed into those
325methods. See `Planet`_ for an example.
326
327
328Restricted subclassing of enumerations
329--------------------------------------
330
331Subclassing an enumeration is allowed only if the enumeration does not define
332any members. So this is forbidden::
333
334 >>> class MoreColor(Color):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700335 ... pink = 17
336 ...
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700337 Traceback (most recent call last):
338 ...
339 TypeError: Cannot extend enumerations
340
341But this is allowed::
342
343 >>> class Foo(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700344 ... def some_behavior(self):
345 ... pass
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700346 ...
347 >>> class Bar(Foo):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700348 ... happy = 1
349 ... sad = 2
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700350 ...
351
352Allowing subclassing of enums that define members would lead to a violation of
353some important invariants of types and instances. On the other hand, it makes
354sense to allow sharing some common behavior between a group of enumerations.
355(See `OrderedEnum`_ for an example.)
356
357
358Pickling
359--------
360
361Enumerations can be pickled and unpickled::
362
363 >>> from test.test_enum import Fruit
364 >>> from pickle import dumps, loads
365 >>> Fruit.tomato is loads(dumps(Fruit.tomato))
366 True
367
368The usual restrictions for pickling apply: picklable enums must be defined in
369the top level of a module, since unpickling requires them to be importable
370from that module.
371
Ethan Furmanca1b7942014-02-08 11:36:27 -0800372.. note::
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700373
Ethan Furmanca1b7942014-02-08 11:36:27 -0800374 With pickle protocol version 4 it is possible to easily pickle enums
375 nested in other classes.
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700376
Ethan Furman2da95042014-03-03 12:42:52 -0800377It is possible to modify how Enum members are pickled/unpickled by defining
378:meth:`__reduce_ex__` in the enumeration class.
379
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700380
381Functional API
382--------------
383
384The :class:`Enum` class is callable, providing the following functional API::
385
386 >>> Animal = Enum('Animal', 'ant bee cat dog')
387 >>> Animal
388 <enum 'Animal'>
389 >>> Animal.ant
390 <Animal.ant: 1>
391 >>> Animal.ant.value
392 1
393 >>> list(Animal)
394 [<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>]
395
Serhiy Storchaka98b28fd2013-10-13 23:12:09 +0300396The semantics of this API resemble :class:`~collections.namedtuple`. The first
397argument of the call to :class:`Enum` is the name of the enumeration.
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700398
399The second argument is the *source* of enumeration member names. It can be a
400whitespace-separated string of names, a sequence of names, a sequence of
4012-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
402values. The last two options enable assigning arbitrary values to
Ethan Furmand9925a12014-09-16 20:35:55 -0700403enumerations; the others auto-assign increasing integers starting with 1 (use
Georg Brandl93a56cd2014-10-30 22:25:41 +0100404the ``start`` parameter to specify a different starting value). A
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700405new class derived from :class:`Enum` is returned. In other words, the above
406assignment to :class:`Animal` is equivalent to::
407
Ethan Furman8a123292015-01-14 22:31:50 -0800408 >>> class Animal(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700409 ... ant = 1
410 ... bee = 2
411 ... cat = 3
412 ... dog = 4
413 ...
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700414
Ethan Furmane2563462013-06-28 19:37:17 -0700415The reason for defaulting to ``1`` as the starting number and not ``0`` is
416that ``0`` is ``False`` in a boolean sense, but enum members all evaluate
417to ``True``.
418
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700419Pickling enums created with the functional API can be tricky as frame stack
420implementation details are used to try and figure out which module the
421enumeration is being created in (e.g. it will fail if you use a utility
422function in separate module, and also may not work on IronPython or Jython).
423The solution is to specify the module name explicitly as follows::
424
Ethan Furman8a123292015-01-14 22:31:50 -0800425 >>> Animal = Enum('Animal', 'ant bee cat dog', module=__name__)
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700426
Ethan Furman2da95042014-03-03 12:42:52 -0800427.. warning::
428
Ethan Furman01cc2d52014-03-03 15:02:04 -0800429 If ``module`` is not supplied, and Enum cannot determine what it is,
Ethan Furman2da95042014-03-03 12:42:52 -0800430 the new Enum members will not be unpicklable; to keep errors closer to
431 the source, pickling will be disabled.
432
Ethan Furmanca1b7942014-02-08 11:36:27 -0800433The new pickle protocol 4 also, in some circumstances, relies on
Larry Hastingsad88d7a2014-02-10 04:26:10 -0800434:attr:`__qualname__` being set to the location where pickle will be able
Ethan Furmanca1b7942014-02-08 11:36:27 -0800435to find the class. For example, if the class was made available in class
436SomeData in the global scope::
437
Ethan Furman8a123292015-01-14 22:31:50 -0800438 >>> Animal = Enum('Animal', 'ant bee cat dog', qualname='SomeData.Animal')
Ethan Furmanca1b7942014-02-08 11:36:27 -0800439
Ethan Furman2da95042014-03-03 12:42:52 -0800440The complete signature is::
441
Ethan Furmand9925a12014-09-16 20:35:55 -0700442 Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>, start=1)
Ethan Furman2da95042014-03-03 12:42:52 -0800443
Ethan Furman01cc2d52014-03-03 15:02:04 -0800444:value: What the new Enum class will record as its name.
Ethan Furman2da95042014-03-03 12:42:52 -0800445
Zachary Waredbd1c432014-03-20 10:01:48 -0500446:names: The Enum members. This can be a whitespace or comma separated string
Ethan Furmand9925a12014-09-16 20:35:55 -0700447 (values will start at 1 unless otherwise specified)::
Ethan Furman2da95042014-03-03 12:42:52 -0800448
Ethan Furman01cc2d52014-03-03 15:02:04 -0800449 'red green blue' | 'red,green,blue' | 'red, green, blue'
Ethan Furman2da95042014-03-03 12:42:52 -0800450
Ethan Furman8a123292015-01-14 22:31:50 -0800451 or an iterator of names::
452
453 ['red', 'green', 'blue']
454
Ethan Furman01cc2d52014-03-03 15:02:04 -0800455 or an iterator of (name, value) pairs::
Ethan Furman2da95042014-03-03 12:42:52 -0800456
457 [('cyan', 4), ('magenta', 5), ('yellow', 6)]
458
Ethan Furman01cc2d52014-03-03 15:02:04 -0800459 or a mapping::
Ethan Furman2da95042014-03-03 12:42:52 -0800460
Zachary Waredbd1c432014-03-20 10:01:48 -0500461 {'chartreuse': 7, 'sea_green': 11, 'rosemary': 42}
Ethan Furman2da95042014-03-03 12:42:52 -0800462
Ethan Furman01cc2d52014-03-03 15:02:04 -0800463:module: name of module where new Enum class can be found.
Ethan Furman2da95042014-03-03 12:42:52 -0800464
Ethan Furman01cc2d52014-03-03 15:02:04 -0800465:qualname: where in module new Enum class can be found.
Ethan Furman2da95042014-03-03 12:42:52 -0800466
Ethan Furman01cc2d52014-03-03 15:02:04 -0800467:type: type to mix in to new Enum class.
Ethan Furman2da95042014-03-03 12:42:52 -0800468
Ethan Furmand9925a12014-09-16 20:35:55 -0700469:start: number to start counting at if only names are passed in
470
Berker Peksag60efd792014-09-18 05:23:14 +0300471.. versionchanged:: 3.5
472 The *start* parameter was added.
473
Ethan Furmanca1b7942014-02-08 11:36:27 -0800474
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700475Derived Enumerations
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700476--------------------
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700477
478IntEnum
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700479^^^^^^^
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700480
481A variation of :class:`Enum` is provided which is also a subclass of
482:class:`int`. Members of an :class:`IntEnum` can be compared to integers;
483by extension, integer enumerations of different types can also be compared
484to each other::
485
486 >>> from enum import IntEnum
487 >>> class Shape(IntEnum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700488 ... circle = 1
489 ... square = 2
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700490 ...
491 >>> class Request(IntEnum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700492 ... post = 1
493 ... get = 2
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700494 ...
495 >>> Shape == 1
496 False
497 >>> Shape.circle == 1
498 True
499 >>> Shape.circle == Request.post
500 True
501
502However, they still can't be compared to standard :class:`Enum` enumerations::
503
504 >>> class Shape(IntEnum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700505 ... circle = 1
506 ... square = 2
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700507 ...
508 >>> class Color(Enum):
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700509 ... red = 1
510 ... green = 2
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700511 ...
512 >>> Shape.circle == Color.red
513 False
514
515:class:`IntEnum` values behave like integers in other ways you'd expect::
516
517 >>> int(Shape.circle)
518 1
519 >>> ['a', 'b', 'c'][Shape.circle]
520 'b'
521 >>> [i for i in range(Shape.square)]
522 [0, 1]
523
524For the vast majority of code, :class:`Enum` is strongly recommended,
525since :class:`IntEnum` breaks some semantic promises of an enumeration (by
526being comparable to integers, and thus by transitivity to other
527unrelated enumerations). It should be used only in special cases where
528there's no other choice; for example, when integer constants are
529replaced with enumerations and backwards compatibility is required with code
530that still expects integers.
531
532
533Others
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700534^^^^^^
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700535
536While :class:`IntEnum` is part of the :mod:`enum` module, it would be very
537simple to implement independently::
538
539 class IntEnum(int, Enum):
540 pass
541
542This demonstrates how similar derived enumerations can be defined; for example
543a :class:`StrEnum` that mixes in :class:`str` instead of :class:`int`.
544
545Some rules:
546
5471. When subclassing :class:`Enum`, mix-in types must appear before
548 :class:`Enum` itself in the sequence of bases, as in the :class:`IntEnum`
549 example above.
5502. While :class:`Enum` can have members of any type, once you mix in an
551 additional type, all the members must have values of that type, e.g.
552 :class:`int` above. This restriction does not apply to mix-ins which only
553 add methods and don't specify another data type such as :class:`int` or
554 :class:`str`.
5553. When another data type is mixed in, the :attr:`value` attribute is *not the
Zachary Waredbd1c432014-03-20 10:01:48 -0500556 same* as the enum member itself, although it is equivalent and will compare
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700557 equal.
Ethan Furmanec15a822013-08-31 19:17:41 -07005584. %-style formatting: `%s` and `%r` call :class:`Enum`'s :meth:`__str__` and
559 :meth:`__repr__` respectively; other codes (such as `%i` or `%h` for
560 IntEnum) treat the enum member as its mixed-in type.
Ethan Furman455bfde2013-09-08 23:48:34 -07005615. :meth:`str.__format__` (or :func:`format`) will use the mixed-in
Ethan Furmanec15a822013-08-31 19:17:41 -0700562 type's :meth:`__format__`. If the :class:`Enum`'s :func:`str` or
563 :func:`repr` is desired use the `!s` or `!r` :class:`str` format codes.
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700564
565
566Interesting examples
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700567--------------------
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700568
569While :class:`Enum` and :class:`IntEnum` are expected to cover the majority of
570use-cases, they cannot cover them all. Here are recipes for some different
571types of enumerations that can be used directly, or as examples for creating
572one's own.
573
574
575AutoNumber
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700576^^^^^^^^^^
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700577
578Avoids having to specify the value for each enumeration member::
579
580 >>> class AutoNumber(Enum):
581 ... def __new__(cls):
582 ... value = len(cls.__members__) + 1
583 ... obj = object.__new__(cls)
Ethan Furman90262622013-07-30 12:24:25 -0700584 ... obj._value_ = value
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700585 ... return obj
586 ...
587 >>> class Color(AutoNumber):
588 ... red = ()
589 ... green = ()
590 ... blue = ()
591 ...
592 >>> Color.green.value == 2
593 True
594
Ethan Furman9a1daf52013-09-27 22:58:06 -0700595.. note::
596
597 The :meth:`__new__` method, if defined, is used during creation of the Enum
598 members; it is then replaced by Enum's :meth:`__new__` which is used after
Ethan Furmanf75805e2014-09-16 19:13:31 -0700599 class creation for lookup of existing members.
Ethan Furman9a1daf52013-09-27 22:58:06 -0700600
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700601
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700602OrderedEnum
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700603^^^^^^^^^^^
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700604
605An ordered enumeration that is not based on :class:`IntEnum` and so maintains
606the normal :class:`Enum` invariants (such as not being comparable to other
607enumerations)::
608
609 >>> class OrderedEnum(Enum):
610 ... def __ge__(self, other):
611 ... if self.__class__ is other.__class__:
Ethan Furman90262622013-07-30 12:24:25 -0700612 ... return self.value >= other.value
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700613 ... return NotImplemented
614 ... def __gt__(self, other):
615 ... if self.__class__ is other.__class__:
Ethan Furman90262622013-07-30 12:24:25 -0700616 ... return self.value > other.value
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700617 ... return NotImplemented
618 ... def __le__(self, other):
619 ... if self.__class__ is other.__class__:
Ethan Furman90262622013-07-30 12:24:25 -0700620 ... return self.value <= other.value
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700621 ... return NotImplemented
622 ... def __lt__(self, other):
623 ... if self.__class__ is other.__class__:
Ethan Furman90262622013-07-30 12:24:25 -0700624 ... return self.value < other.value
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700625 ... return NotImplemented
626 ...
627 >>> class Grade(OrderedEnum):
628 ... A = 5
629 ... B = 4
630 ... C = 3
631 ... D = 2
632 ... F = 1
633 ...
634 >>> Grade.C < Grade.A
635 True
636
637
Ethan Furmanf24bb352013-07-18 17:05:39 -0700638DuplicateFreeEnum
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700639^^^^^^^^^^^^^^^^^
Ethan Furmanf24bb352013-07-18 17:05:39 -0700640
641Raises an error if a duplicate member name is found instead of creating an
642alias::
643
644 >>> class DuplicateFreeEnum(Enum):
645 ... def __init__(self, *args):
646 ... cls = self.__class__
647 ... if any(self.value == e.value for e in cls):
648 ... a = self.name
649 ... e = cls(self.value).name
650 ... raise ValueError(
651 ... "aliases not allowed in DuplicateFreeEnum: %r --> %r"
652 ... % (a, e))
653 ...
654 >>> class Color(DuplicateFreeEnum):
655 ... red = 1
656 ... green = 2
657 ... blue = 3
658 ... grene = 2
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700659 ...
Ethan Furmanf24bb352013-07-18 17:05:39 -0700660 Traceback (most recent call last):
661 ...
662 ValueError: aliases not allowed in DuplicateFreeEnum: 'grene' --> 'green'
663
664.. note::
665
666 This is a useful example for subclassing Enum to add or change other
Ezio Melotti93d7dda2013-10-05 04:13:18 +0300667 behaviors as well as disallowing aliases. If the only desired change is
Ezio Melotti17f1edd2013-10-05 04:26:06 +0300668 disallowing aliases, the :func:`unique` decorator can be used instead.
Ethan Furmanf24bb352013-07-18 17:05:39 -0700669
670
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700671Planet
Ethan Furmaned0bf8a2013-09-06 19:53:30 -0700672^^^^^^
Ethan Furman6b3d64a2013-06-14 16:55:46 -0700673
674If :meth:`__new__` or :meth:`__init__` is defined the value of the enum member
675will be passed to those methods::
676
677 >>> class Planet(Enum):
678 ... MERCURY = (3.303e+23, 2.4397e6)
679 ... VENUS = (4.869e+24, 6.0518e6)
680 ... EARTH = (5.976e+24, 6.37814e6)
681 ... MARS = (6.421e+23, 3.3972e6)
682 ... JUPITER = (1.9e+27, 7.1492e7)
683 ... SATURN = (5.688e+26, 6.0268e7)
684 ... URANUS = (8.686e+25, 2.5559e7)
685 ... NEPTUNE = (1.024e+26, 2.4746e7)
686 ... def __init__(self, mass, radius):
687 ... self.mass = mass # in kilograms
688 ... self.radius = radius # in meters
689 ... @property
690 ... def surface_gravity(self):
691 ... # universal gravitational constant (m3 kg-1 s-2)
692 ... G = 6.67300E-11
693 ... return G * self.mass / (self.radius * self.radius)
694 ...
695 >>> Planet.EARTH.value
696 (5.976e+24, 6378140.0)
697 >>> Planet.EARTH.surface_gravity
698 9.802652743337129
Ethan Furman9a1daf52013-09-27 22:58:06 -0700699
700
701How are Enums different?
702------------------------
703
704Enums have a custom metaclass that affects many aspects of both derived Enum
705classes and their instances (members).
706
707
708Enum Classes
709^^^^^^^^^^^^
710
711The :class:`EnumMeta` metaclass is responsible for providing the
712:meth:`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that
713allow one to do things with an :class:`Enum` class that fail on a typical
714class, such as `list(Color)` or `some_var in Color`. :class:`EnumMeta` is
715responsible for ensuring that various other methods on the final :class:`Enum`
716class are correct (such as :meth:`__new__`, :meth:`__getnewargs__`,
717:meth:`__str__` and :meth:`__repr__`)
718
719
720Enum Members (aka instances)
721^^^^^^^^^^^^^^^^^^^^^^^^^^^^
722
723The most interesting thing about Enum members is that they are singletons.
724:class:`EnumMeta` creates them all while it is creating the :class:`Enum`
725class itself, and then puts a custom :meth:`__new__` in place to ensure
726that no new ones are ever instantiated by returning only the existing
727member instances.
728
729
730Finer Points
731^^^^^^^^^^^^
732
733Enum members are instances of an Enum class, and even though they are
734accessible as `EnumClass.member`, they are not accessible directly from
735the member::
736
737 >>> Color.red
738 <Color.red: 1>
739 >>> Color.red.blue
740 Traceback (most recent call last):
741 ...
742 AttributeError: 'Color' object has no attribute 'blue'
743
Ezio Melotti93d7dda2013-10-05 04:13:18 +0300744Likewise, the :attr:`__members__` is only available on the class.
Ethan Furman9a1daf52013-09-27 22:58:06 -0700745
Ezio Melotti93d7dda2013-10-05 04:13:18 +0300746If you give your :class:`Enum` subclass extra methods, like the `Planet`_
747class above, those methods will show up in a :func:`dir` of the member,
748but not of the class::
Ethan Furman9a1daf52013-09-27 22:58:06 -0700749
750 >>> dir(Planet)
751 ['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__']
752 >>> dir(Planet.EARTH)
753 ['__class__', '__doc__', '__module__', 'name', 'surface_gravity', 'value']
754
Ethan Furmanf75805e2014-09-16 19:13:31 -0700755The :meth:`__new__` method will only be used for the creation of the
756:class:`Enum` members -- after that it is replaced. Any custom :meth:`__new__`
757method must create the object and set the :attr:`_value_` attribute
758appropriately.
759
760If you wish to change how :class:`Enum` members are looked up you should either
761write a helper function or a :func:`classmethod` for the :class:`Enum`
762subclass.