Python Enum's

Python Enum's

@wannahackin



Enumeration is a kind of data type that represents a set of named values; each of these values corresponds to some constant.

So, basically, an enum is a way to describe possible constant values for some variable.

The main idea here is that enum members are related to some semantic domain, or more correct to say – it is a distinct set of related and semantically meaningful values or options within a specific context.

For example, days of the week could be grouped in one enum, and member values can be used later in code somewhere as the "today" variable value.

The great thing about enums is that they are an immutable data type. We will talk about the benefits of immutable data in the next blog :) for now, just keep in mind that immutable data types are a GOOD thing, at least for constants in our context today.

Besides that, it also guarantees the type safety of members and provides an overall good way to improve the readability of code and make it more maintainable.


In Python, enums are represented by the Enum class in the standard lib module called "enum".

To create our own enum, we should inherit from Enum class like this:


Because enums are iterable collections, it was possible for me to print it as a list() in the example above.

And this is how enum members can be accessed in several ways:


Enum member values do not strictly have to be integers. Come on, it is Python, after all!


And even such weird things:


So, as I mentioned, enums are immutable; we cannot reassign their members.

But in this strange example we can use all methods of enum members, because their "values" are instances of "SomeCustomDataType".

Enum, in such a case, just provides us with guarantees that every particular instance linked to enum member will stay the same.

I doubt real cases because if you need only one instance of a class, you will probably treat it as a singleton in the constructor, so the code in the last and next examples is just for showing things :)
The instance of SECRET_STRING is immutable in enum, but enum does not dive a fuck of internal properties of SomeCustomDataType, so they are mutable.

Enum members have name and value properties, so we are able to access our class methods in enum members' via the member "value" prop.


Enum members support comparison operators such as == and !=


The good thing about enums is that even if you have two similar enums with the same member names and values, they are still different enums, and their members have unique identifiers:

Logically and semantically notes are the same, yes. But it should sound "different" because of an instrument.


Besides that, we can define some methods in enums, so it is not just a storage for constants.



Last example, I promise :)

Let’s take a look at a more "real-life" enum usage – kind of a state machine :)

It is a pretty common example across the web, probably because no one wants to show the horrible, complicated stuff they're doing with enums in real projects :D


Let's write a state machine for a Coffee Machine and a class implementing some Coffee Machine functionality:



Using `auto()` as member values here is a way for us to not assign a specific value to them because what value can adequately represent a STATE representation?

It is a concept in itself, and semantically clear to understand by member name.

`auto()`'s default behavior under the hood is to assign consecutive integers to every enum member, and it is pretty handy in such cases.


I hope this material finds you well, and you understand that enums might be pretty handy in development :)

Thanks for sharing this blog with someone you love, bye :)

Report Page