Python 3, an Illustrated Tour Transcripts
Chapter: The standard library
Lecture: Enum
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In this video, we're going to talk about the enum library, this came in Python 3.4 with Pep 435.
0:06
In the PEP we read, enumeration is a set of symbolic names bound to unique constant values.
0:11
Within an enumeration, the values can be compared by identity and the numeration itself can be iterated over.
0:18
If you're not familiar with enumerations in other languages, they allow you to hard-code magic numbers and make use of those
0:24
and you could do that in Python prior by making globals and making all capitalized variable name and setting equal to some value.
0:32
There are a few features that enumerations have that make them slightly better to use. Here's an example.
0:37
I'm going to import the enum class from the enum module and then I just define a class in this case, I'm going to define a class called bike
0:45
and there are various types of bikes. So there might be a road bike or mountain biker or a cross bike or a trike.
0:50
and maybe I'm going to be switching on these different bike types or whatnot. Inside of my class, as attributes I say road is equal to 1,
0:58
mountain is equal to 2, etc. And I can define numbers that give values for those.
1:05
If you want to enumerate all the different possibilities of what are in a bike you can loop over that and you can say
1:12
well there's road, mountain, cross and trike, you can also do comparisons using the equality operator. So the last bike in the enumeration was trike,
1:21
and is that equal to a bike.trike, yes, that is the case. Trike is I believe number 4 here so you could say is bike equal equal to 4.
1:32
That's what we're trying to get around. We're trying to get around magic numbers where you're using number that has a unique meaning for you,
1:39
but maybe to someone else who's reading it doesn't make sense. So bike.trike is very explicit and makes the code more readable.
1:46
If we want to access these enumerations, you can access them in different ways so you can do the by attribute, so you can just say .mountain
1:54
you can also say bike 2, you can call it and pass in 2 and that will give you what the enumeration is. You can also do it by index name.
2:04
So there's an index operation that says mountain and that gives you back the enumeration. All these are the same.
2:11
I prefer this first one I think it's the most readable. Identity comparisons also work with enumerations as well.
2:16
So you can say bike.mountain is bike.mountain and that is true. So it's not going to make a new instance of those.
2:22
There's an alternate construction that we can use to create enumerations here. This is similar to the named tuple construction.
2:28
We're going to make a variable here called bike or bike2, camel case
2:33
because it's class like and then we're going to pass in the name of the class here. And then we're going to pass in the different enumerations in here.
2:41
And in this case, we don't need to provide the numbers we'll get default numbers for them,
2:46
so I can say bike2 what's the 2, the 2 was in this case mountain and what is road and that was this one right here bike road, which has a value of 1.
2:58
This video discussed enumerations in Python. This is included in Python 3. This is just a little library that's meant to make your code more readable.
3:06
If you're using hard coded numbers all over the place consider using enumerations, or if you have different categorical types that you're using
3:15
consider using enumerations to make your code more readable.