Creating Enumerations in Python
00:00
Creating enumerations with Python’s enum
Python’s Enum
module provides the Enum
class, which allows you to create enumeration types.
00:10
To create your own enumerations, you can either subclass Enum
, or use its functional API. Both options will let you define a set of related constants as Enum
members.
00:22
The Enum
module defines a general purpose enumeration type with iteration and comparison capabilities. You can use this type to create sets of named constants that you can use to replace literals of common data types, such as numbers and strings.
00:38
A classic example of when you should use an enumeration is when you need to create a set of enumerated constants representing the days of the week. Each day will have a symbolic name and a numeric value between one and seven inclusive. On screen you can see how to create this enumeration by using Enum
as your superclass or parent class.
01:03
Day
class is a subclass of Enum
, so you can call Day
an enumeration or just an Enum
. Day.MONDAY
, Day.TUESDAY
, and the like.
01:13
Are enumeration members also known as Enum
members or just members? Each member must have a value which needs to be constant. Often the values mapped to members are consecutive integer numbers, however, they can be of any type, including user-defined types.
01:32
In this example, the value of Day.MONDAY
is one, Day.TUESDAY
is two, and so on. Because enumeration members must be constants, Python doesn’t allow you to assign new values to members at runtime.
01:46
If you try to change the value of a member, you get an AttributeError
.
01:53
You may have noticed that the members of Day
are capitalized and onscreen you can see why. You can think of enumerations as collections of constants.
02:05
Like lists, tuples, or dictionaries, Python’s enumerations are also iterable. That’s why you can use list()
to turn any enumeration into a list of enumeration members.
02:17 The members of a Python enumeration are instances of the container enumeration itself.
02:27
You shouldn’t confuse a custom Enum
class such as Day
with its members Day.MONDAY
, Day.TUESDAY
, and so on.
02:35
In this example, the Day
Enum
type is a hub for enumeration members, which happen to be of the type Day
. Unlike member names, the name containing the enumeration itself isn’t a constant but a variable.
02:50
So it’s possible to rebind this name at any moment during your program’s execution, but you should avoid doing that as you’ve just seen. You’ve reassigned Day
, which now holds a string rather than the original enumeration.
03:05 By doing this, you’ve lost the reference to the enumeration itself.
03:12
You can also use range
to build enumerations
03:23
Here. range
is used with the start and stop arguments. The start argument allows you to provide the number that starts the range, while the stop argument defines the number at which the range will stop generating numbers.
03:38
Even though you use the class syntax to create enumerations, they’re special classes that differ from normal Python classes. Unlike regular classes, Enum
s can’t be instantiated can’t be subclassed unless the base Enum
has no members provide a human-readable string representation for their members.
03:58 They’re iterable, returning their members in a sequence. They provide hashable members that can be used as dictionary keys. They support the square bracket syntax, call syntax, and dot notation to access their members, and they don’t allow member reassignments.
04:16 You should keep all these subtle differences in mind when you start creating and working with your own enumerations in Python. While members often take consecutive integer values, it’s possible that the values can be of any type, including user-defined types.
04:32 Here’s an enumeration of school grades that uses non-consecutive numeric values in descending order.
04:44 This shows that Python enums are flexible and allow you to use any meaningful value for their members. You can set the member values according to the intent of your code.
04:56 You can also use string values for members. Here’s an example of a size enumeration that you can use in an online store.
05:12 The value associated with each size holds a description that can help you and other developers understand the meaning of your code.
05:22 In the next section of the course, you’ll take a look at the creation of enumerations in more depth.
Become a Member to join the conversation.