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.
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.
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.
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:
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.
Let us now look at an example to understand this better.
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.
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:
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.
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.
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
|Python Training||Sep 27 to Oct 12|
|Python Training||Oct 01 to Oct 16|
|Python Training||Oct 04 to Oct 19|
|Python Training||Oct 08 to Oct 23|
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.
Copyright © 2013 - 2022 MindMajix Technologies