Exploring Other Enumeration Classes
00:00
Exploring other enumeration classes. Apart from Enum
, the enum
module provides a few additional classes that allow you to create enumerations with specific behaviors.
00:12
You’ll have the IntEnum
class for creating enumerated constants that are also subclasses of int
, which implies that all members will have the features of an integer number.
00:21
In Python 3.11 and later, there’s the StrEnum
class for creating enums that are subclasses of the built-in str
type. You’ll also find more specialized classes like IntFlag
and Flag
.
00:34 Both classes will allow you to create enumerated sets of constants that you can combine using bitwise operators. In this section of the course, you’ll explore these classes and how they work in Python.
00:47
Integer enumerations are so common that the Enum
module exports a dedicated class called IntEnum
that was specifically created to cover this use case.
00:56
If you need the members of your enumerations to behave like integer numbers, then you should inherit from IntEnum
rather than from Enum
.
01:06
Subclassing IntEnum
is equivalent to using multiple inheritance with int
as the mixin class.
01:19
Now Size
inherits directly from IntEnum
instead of int
and IntEnum
like the previous version of Size
.
01:26 This new version has full comparison capabilities and supports all the comparison operators. You can also use the class members in integer operations directly.
01:45
Size
will automatically attempt to convert any value of a different data type to an integer number. If this conversion isn’t possible, then you’ll get a ValueError
02:03
here. Size
automatically converts the string “4” into an integer value,
02:16
but here, because the string “4.0” doesn’t hold a valid numeric value, you get a ValueError
and the conversion fails.
02:27
In Python version 3.10, the Enum
module didn’t include a string class, but this class is another example of a popular use case of enumerations and for this reason, Python 3.11 and later include a StrEnum
type with direct support for common string operations.
03:15
If you’re unable to use Python 3.11 or later, you can simulate the behavior of a StrEnum
class by creating a mixin class with str
and Enum
as parent classes.
03:27
You can use IntFlag
as a base class for enumerations that support the bitwise operators. Performing bitwise operations on members of an IntFlag
subclass will return an object that’s also a member of the underlying enum.
03:42
Here’s an example of a Role
enumeration that lets you manage different user roles in a single combined object.
03:52 In this code, you create an enumeration that holds a set of user roles in a given application. The members of this enumeration hold integer values that you can combine using the bitwise OR operator.
04:09
For example, the username John has both the USER
and SUPERVISOR
roles. Note that the object stored in john_roles
is a member of the Role
enumeration.
04:45
You should keep in mind that individual members of enums based on IntFlag
, also known as flags, should take values that are powers of two. However, this isn’t a requirement for combination of flags as seen here with Role.ADMIN
.
05:00
You defined Role.ADMIN
as a combination of roles. Its value results from applying the bitwise OR operator to the complete list of previous roles in the enumeration.
05:12
IntFlag
also supports integer operations such as arithmetic and comparison operations. But these types of operations return integers rather than member objects.
05:30
IntFlag
members are also subclasses of int
. That’s why you can use them in expressions that involve integer numbers. In these situations, the resulting value will be an integer rather than an Enum
member.
05:45
Finally, you’ll also find that Flag
class available in enum
. This class works similarly to IntFlag
, but has some additional restrictions.
06:42
The main difference between IntFlag
and Flag
is that the latter doesn’t inherit from int
. Therefore, integer operations are not supported.
06:51
When you try to use a member of Role
in an integer operation, you get a TypeError
.
07:00
Just like members of IntFlag
enums, the members of Flag
enums should have values that are powers of two. Once again, this doesn’t apply to combinations of flags such as ROLE_ADMIN
.
07:11 In the example seen earlier, Python’s enumerations can help you improve your code’s readability and organization. You can use them to group similar constants that you can use in your code to replace strings, numbers, and other values with readable and meaningful names.
07:32
In the next section of the course, you’ll code a couple of practical examples that deal with common IntEnum
and Enum
use cases to help you decide when your code could benefit from using enumerations.
Become a Member to join the conversation.