Python enum

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.

If you would like to become a Python certified professional, then visit Mindmajix - A Global online training platform:  Python Training”  Course.  This course will help you to achieve excellence in this domain.

Enum in Python

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.

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.

 MindMajix YouTube Channel

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.

[Related Page: Python Enumerate with Example

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 are understand with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

If you are interested to learn Python and to become an Python Expert? Then check out our Python Certification Training Course at your near Cities.

Python Course ChennaiPython Course BangalorePython Course DallasPython Course Newyork

These courses are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in Python and help you to achieve your dream job.

Check out Python Sample Resumes

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Python TrainingNov 19 to Dec 04View Details
Python TrainingNov 23 to Dec 08View Details
Python TrainingNov 26 to Dec 11View Details
Python TrainingNov 30 to Dec 15View Details
Last updated: 03 Apr 2023
About Author

Anjaneyulu Naini is working as a Content contributor for Mindmajix. He has a great understanding of today’s technology and statistical analysis environment, which includes key aspects such as analysis of variance and software,. He is well aware of various technologies such as Python, Artificial Intelligence, Oracle, Business Intelligence, Altrex, etc. Connect with him on LinkedIn and Twitter.

read less
  1. Share:
Python Articles