GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python enumerations
Python enumerationsThis article is created to provide you with an easy way to learn "enum" (enumeration) in Python with the help of a practical example. This article deals with:
Definition of enumerations in PythonEnumeration in Python is simply a collection of symbolic names that are bound to unique and constant values. An enumeration in Python can be iterated. The example to show how enumeration in Python can be iterated is shown later on in this page. The "enum" module is used to implement enumeration in Python. And classes are used to create enumerations, like shown in the code block given below: from enum import Enum class OrderCount(Enum): LAPTOP = 3 MOBILE = 6 TV = 2 Note: In the above example, I've written all the enum members in UPPERCASE letters because enumeration is used to represent constants. And it is better practice to write any variable that holds constant values in uppercase. Otherwise, it is up to you whether to use uppercase, lowercase, or a combination of the two. In the above example of enumeration:
Enumeration Methods and PropertiesNow we've got to learn these 5 things that are most widely used in Python enumerations. I kept the above short block of code in mind while evaluating these things with the help of an example to give you a clear idea of the subject.
Implementation of enumeration methods and properties as an exampleThe program given below uses repr(), type(), isinstance(), name, and value in one single program. Let's have a look at the program given below and understand it in a practical way after seeing its sample output: from enum import Enum class OrderCount(Enum): LAPTOP = 3 MOBILE = 6 TV = 2 print(repr(OrderCount.LAPTOP)) print(type(OrderCount.LAPTOP)) print(isinstance(OrderCount.LAPTOP, OrderCount)) print(OrderCount.LAPTOP.name) print(OrderCount.LAPTOP.value) Here is its sample output: Enumeration implementation exampleNow let's take a look at the program given below that demonstrates enumeration in a practical way. This is basically a modified version of the previous program. Since this program includes a description of each output: from enum import Enum class OrderCount(Enum): LAPTOP = 3 MOBILE = 6 TV = 2 print("The string representation of enum member \"OrderCount.MOBILE\" is:", OrderCount.MOBILE) print("The \"repr\" representation of enum member \"OrderCount.MOBILE\" is:", repr(OrderCount.MOBILE)) print("The name of enum member \"OrderCount.MOBILE\" is:", OrderCount.MOBILE.name) print("The value of enum member \"OrderCount.MOBILE\" is:", OrderCount.MOBILE.value) The snapshot given below shows its sample output: Print all enumeration values using "enum" iterationThis example is created in such a way that it will create an enumeration and then print all the values of the enumeration: from enum import Enum class OrderCount(Enum): LAPTOP = 3 MOBILE = 6 TV = 2 print("All enum values are:") for val in OrderCount: print(val.value) This program produces the output that looks like: Print all the enumeration items in name-value pairsBasically, this is a modified and improved version of the previous program. This program prints all enumeration items' names along with their values in an organized way: from enum import Enum class OrderCount(Enum): LAPTOP = 3 MOBILE = 6 TABLET = 2 print("All enum Names - values are:") print("\nName\t\tValue") print("-----------------") for val in OrderCount: print(val.name, "\t\t", val.value) And the snapshot given below shows the sample output produced by the above Python program: Enumeration members are hashableSince enumeration members in Python are hashable, we can use them in dictionaries or sets. If any object never changes its value during its lifetime, then the object is called a hashable object. In other words, hashability enables an object to be usable as a dictionary key and a set member. The example program given below illustrates that all the things said here to support enum members are hashable. from enum import Enum class Product(Enum): MOBILE = 3 LAPTOP = 6 TABLET = 5 product_dict = {} product_dict[Product.MOBILE] = 'white' product_dict[Product.LAPTOP] = 'black' if product_dict == {Product.MOBILE: 'white', Product.LAPTOP: 'black'}: print("Enum members are hashable") else: print("Enum members are not hashable") The image below shows an example of the output this Python program produces: « Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |