Blog

Python enum

  • (4.0)
  •   |   471 Ratings

Introduction:

Python is a powerful programming language that has started regaining its fame for its usage in the Data Science along with the latest technologies like R and etc. Having said that, let us take a look at the tiny winy bits of concepts to get ourselves stronger in this programming language.

In this article, we will try to understand the concept of enum in Python programming language. To provide you a better understanding on what enum can be used for in Python programming language, take a look at the following section.

Python enum:

enum, stands for an enumeration – it is an symbolic representation of set of constants mapped against bound to unique names. The enumeration itself can be iterated over and this can be compared by identity as well. Python programming language provides two enumeration classes which can be used to create unique sets of names and constant values mapped against those names – Enum and IntEnum.

Class enum.Enum is a base class provided for creating enumerated constants

Class enum.IntEnum is a base class provided for creating enumerated constants that are subclasses to int as well.

enum.unique() is a decorator provided to check that there is only one name that is bound to any given single value.

Related Page: Comparing R Language Vs Python

Let us take a look a basic enum creation without using any of the constructs provided by the Python programming language itself.


Basic enum creation

If you observe the class that we have created earlier, it just holds three unique constants with three unique values. When we are referring to a constant, we are referring it by the class name. So, to understand the concept of enums, it is just a class that contains unique variables assigned unique values for usage in further stages. This is how we can create an enum without even using the constructs provided by the Python programming language.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

To be precise, there is some non-trivial value that enums bring to your actual Python code. Enums become very apparent in scenarios where there is more of persistence to databases. To change the same plain old class file to an enum construct, using the libraries provided by Python programming language – the corresponding change to the class would look something as like below:

Also see that the output of the print command shows, the variable used against the value of it.

Variable print

Check out Python Interview Questions

Now let us extend the same example and try to see the way how we can extract the constant name and the constant value individually, by means provided by Python programming language. This can be done by using name and value parameters of the member construct provided by Python. Let us now look at an example to understand it better:

Parameters of the member

Now, let us understand a key feature why we use enums in the first place. It is a known fact as we have introduced about enums provided by Python, but it is noteworthy that two enum members can hold same values but the other way round is not possible – an enum member cannot hold two values in an enum.

Considering that two enum members (T20, IPL in our example and T20 defined first) are assigned same values, IPL becomes an alias to T20. By value lookup of the value of T20 and IPL will return the value of T20 alone. By lookup of value of IPL also returns the value of T20. Observe the difference when we use either the original enum member or the alias enum member, the value that will get returned will be the same and always the original enum member will be returned back as an output.

Related Page: Machine Learning With Python And Its Basics That Is Necessary To Master

Let us now look at an example to understand this better.

Original enum member
What happens when you want to have just one unique enum member holding the value but not to have another enum member acting as an alias? That’s when we use the @unique identifier to let know Python that there is a strict check on not to have a duplicate enum member. Let us now take a look at an example that portrays the same.
Duplicate enum number

The error above clearly states that we cannot have another enum member using the same value as mentioned earlier. This ensures that there are no duplicates to use in your enums in Python.

Now let us take a look at how we can use two different members of an enum to do some comparisons between them. These comparisons can be useful in your programming, but not in your enum construct as such. Let us now check this example to understand the topic clearer:

enum construct

Conclusion:

In this article, we have seen what cases enum can be used and why are they used in Python programming language. We have also tried to take a deeper look into the concept with various examples as well.

Hope that you were clear with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

Check out Python Sample Resumes


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules