Exploring Other Enumeration Classes
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.
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.
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: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.
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
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
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.
In the next section of the course, you’ll code a couple of practical examples that deal with common
Enum use cases to help you decide when your code could benefit from using enumerations.
Become a Member to join the conversation.