Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Comparing and Sorting Enumerations

00:00 Comparing and sorting enumerations. Being able to use enumerations in if statements and match case statements suggest that enumeration members can be compared by default.

00:14 Enums support two types of comparison. Operator identity using the is and is not operators and equality using the equal to and not equal to operators.

00:27 The identity comparison relies on the fact that each enum member is a singleton instance of its enumeration class. This characteristic allows a fast and cheap identity comparison of members using the is and is not.

00:41 Operators consider these examples which compare different combinations of enum members.

01:23 Every enum member has its own identity, which is different from the identity of its sibling members. This rule doesn’t apply to member aliases because they’re just references to existing members and share the same identity.

01:36 This is why comparing red and pedestrian red returns true. Identity checks between members of different enumerations always return false.

02:06 The reason for this false result is that members of different enums are independent instances with their own identities, so any identity check on them returns false.

02:17 The equality operators also work between enumeration members.

02:33 Python’s enumerations support both operators by delegating to the is and is not operators.

02:57 As you already learned, enum members always have a concrete value that can be a number, a string, or any other object. Because of this, running equality comparisons between enum members and common objects can be tempting, but this kind of comparison doesn’t work as expected because the actual comparison is based on object identity,

03:20 even though the member values are equal to the integers. In each example, these comparisons return false. This is because regular enum members compare by object identity rather than by value.

03:32 In the example, just seeing you’re comparing enum members to integer numbers, which is like comparing apples and oranges, they’ll never compare equally because they have different identities.

03:43 Later you’ll learn about the IntEnum special enumeration, which can be compared to integers. Finally, another comparison related feature of enumerations is that you can perform membership tests on them using the in and not in operators.

04:03 Python’s enumerations support the in and not in operators by default. Using these, you can check if a given member is present in an given enumeration.

04:15 By default, Python’s enums do not support comparison operators such as greater than, less than, and so on. That’s why the attempt to use the built-in sorted() function seen on screen will fail.

04:29 When you use an enumeration as an argument to sort it, you get a TypeError Because enums don’t support the less than operator. However, there’s a way to successfully sort enumerations by their members’ names and values using the key argument in the sorted() call.

04:45 Here’s how you do it.

04:51 You use a lambda function that takes an enumeration member as an argument and returns its .value attribute. With this technique, you can sort the input enumeration by its values.

05:03 In this second example, the lambda function takes an enum member and returns its .name attribute. That way you can sort the enumeration by the names of its members.

05:17 So far, you’ve learned how to create and use enumerations in Python. Up to this point, you’ve worked with default enumerations. This means you’ve used Python’s enumerations with their standard features and behaviors only.

05:30 Sometimes you may need to provide your enumerations with custom behavior. To do this, you can add methods to your enums and implement the required functionality.

05:40 You can also use mixin classes.

05:45 In the next part of the course, you’ll learn how to take advantage of both techniques to customize your enumerations.

Become a Member to join the conversation.