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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using Automatic Values, Aliases, and Unique Values

00:00 Automatic values, aliases, and unique values. Python’s enum module provides a convenient function called auto() that allows you to set automatic values for your Enum members.

00:13 Its default behavior is to assign consecutive integer values. On screen you can see how auto() works.

00:26 You need to call auto() once for each automatic value that you need. As you can see, you can also combine auto() with concrete values, such as for Wednesday and Sunday.

00:44 By default, auto() assigns consecutive integer numbers to each Enum member starting from one. You can tweak this default behavior by overriding the methods seen on screen, which auto() uses under the hood to generate the automatic values.

01:02 Here’s an example of how to do this.

01:09 Here you create an enumeration of Earth’s cardinal directions in which values are automatically set to strings containing the first character of each member’s name.

01:18 Note that you must provide your overridden version of ._generate_next_value_() before defining any members. That’s because the members will be built by calling the method.

01:35 You can create enumerations in which two or more members have the same constant value. The redundant members are known as aliases and can be useful in some situations.

01:46 For example, let’s say you have an Enum containing a set of operating systems as seen on screen.

02:00 Linux distributions are considered as independent operating systems. So Ubuntu and Debian are both independent systems with different goals and target audiences.

02:10 However, they share a common kernel called Linux. These enumerations map operating systems to their corresponding kernels. This relationship turns Debian into an alias of Ubuntu, which may be useful when you have code that’s kernel related, along with code that’s specific to a given Linux distribution.

02:30 An important piece of behavior to note in this example is when you iterate over the Enum directly, aliases aren’t considered. If you ever need to iterate over all the members, including aliases, then you need to use .__members__.

02:46 You’ll learn more about iteration and the .__members__ attribute later on in the course. You also have the option to completely forbid aliases in your enumerations.

02:57 To do this, you use the @unique decorator from the enum module here. You use @unique to decorate the OperatingSystem enum.

03:10 If any member value is duplicated, then you get a ValueError.

03:22 Here the exception message points out that Debian and Ubuntu share the same value, which is not allowed. Up to this point, you’ve learned what enumerations are, when to use them, and what benefits you get from using them in your code.

03:39 You’ve also learned how to create enumerations in Python using the Enum class, either as a superclass or as a callable.

03:47 Now it’s time for you to start digging into how Python’s enumerations work and how you can use them in your code. So in the next section of the course, that’s what you’ll be doing.

Become a Member to join the conversation.