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

Working With Enumerations in Python

00:00 Working with Enumerations in Python. When it comes to using enumerations in your code, accessing their members is a fundamental operation to perform. You’ll have three different ways to access enumeration members in Python.

00:16 Let’s say you need to access the north member of the Cardinal direction enumeration on screen.

00:23 This shows how you can access an Enum member using the dot notation, which is pretty intuitive and readable. This line accesses the target member by calling the enumeration with the member’s value as an argument.

00:37 And a third access method is to use subscript notation. Python’s Enumerations offer great flexibility for you to access members. The dot notation is arguably the most commonly used approach in Python code, but the other two approaches can be helpful as well.

00:56 So use the notation that fulfills your specific needs, conventions, and style. It’s important to note that calling an enumeration with a member’s value as an argument can make you feel like you are instantiating the enumeration.

01:10 But as you already know, enumerations can’t be instantiated.

01:18 Trying to create an instance of an existing enumeration isn’t allowed, so you get a TypeError if you attempt to do it. So you mustn’t confuse instantiating with accessing members through an enumeration call.

01:32 The members of a Python enumeration are instances of their containing class. During the Enum class parsing, each member is automatically provided with a .name attribute that holds the member’s name as a string. Members also get a .value attribute that stores the value assigned to the member itself.

01:49 In the class definition, you can access .name and .value as you would do with a regular attribute using the dot notation. Take a look at this example, which simulates a semaphore, commonly known as a traffic light.

02:06 The .name and .value attributes of an Enum member give you direct access to the member’s name as a string and to the member’s value, respectively.

02:15 These attributes come in handy when you’re iterating through your enumerations, which you’ll explore next. One useful feature of Python’s enumerations compared to regular classes is that enumerations are iterable by default.

02:30 Because of this, you can use them in for loops and with other tools that accept and process iterable objects. Python’s enumerations support direct iteration over members in the definition order.

02:48 In this example, you use a for loop to iterate over the members of Flavor. Note that members are produced in the same order as they were defined in the class definition.

02:59 When you are iterating over an enumeration, you can access the .name and .value attributes as you go.

03:09 This looks pretty similar to iterating over a dictionary, so if you are familiar with dictionary iteration, then looping over enumerations using this technique will be a straightforward task with many potential use cases.

03:23 Alternatively, enumerations have a special attribute called .__members__ that you can also use for iterating over their members. This attribute holds a dictionary that maps names to members.

03:35 The difference between iterating over this dictionary and the enumeration directly is that the dictionary gives you access to all members of the enumeration, including all of the aliases that you may have.

03:46 Because .__members__ holds a regular dictionary, you can use all the iteration techniques that apply to this built-in data type.

03:58 Some of these techniques include using dictionary methods such as .keys()

04:06 .values()

04:16 and .items().

04:30 In the next section of the course, you’ll look at how to use enumerations in if and match statements.

Become a Member to join the conversation.