Javatpoint Logo
Javatpoint Logo

Enum class in Python

Enum is the class in Python used for developing the enumerations. Enumeration is the set of symbolic members or names which are bounded to the constant and unique values. The members of the enumeration can be equated by using these symbolic names. The enumeration can be repeated by itself over them.

The characteristics of the Enum class are:

  • The users can check the types of Enum by using the type() method.
  • By using the 'name' keyword, the users can display the name of the Enum.
  • The Enum is the evaluable string representation of the object known as repr().

Example:

Output:

The member of Enum class as the string is :  Weekdays.Monday
The member of Enum class as a repr is :  
The type of the member of Enum class is :  
The name of the member of Enum class is :  Friday

How to print Enum as an Iterable list

The users can print the Enum class as an iterable list.

In the following example, we will use for loop to print all the members of the Enum class.

Example:

Output:

The member of Enum class are : 
Weekdays.Sunday
Weekdays.Monday
Weekdays.Tuesday
Weekdays.Wednesday
Weekdays.Thursday
Weekdays.Friday
Weekdays.Saturday

How to Hash Enum Class

The members of the Enum class are called Enumeration, and also hashable. Therefore, these members can be used for sets and dictionaries.

In the following example, we will show how users can hash the Enum class.

Example:

Output:

Enum class is hashed

How to access Enum members

The users can access the members of the Enum class by using the value or the name of the member items.

In the following example, we will show how the users can access the value by name where we have used the name of the Enum as an index.

Example:

Output:

The member of Enum class accessed by name: 
Days.Monday
The member of Enum class accessed by name: 
Days.Friday
The member of Enum class accessed by Value: 
Days.Sunday
The member of Enum class accessed by Value: 
Days.Thursday

How to Compare the Enums

The users can compare the Enums simply by using the comparison operator.

For example:

Output:

Match
Do not Match
Match
Do not Match

Conclusion

In this article, we have discussed the Enum class of Python and its characteristics. We also showed how the user could print the Enum class as an iterable list. How the users can hash and access the members of the Enum class.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA