Change language

enum in Python

| | |

Enumeration properties:

1. Enumerations can be displayed as string or repr .

2. Enumerations can be type checked using type () .

3. The keyword " name " is used to display the name of an enumeration member.

# Python code to demonstrate enumerations

  
# enum import for enums

import enum

 
# creating enums using the class

class Animal (enum.Enum):

dog = 1

cat = 2

  lion = 3

 
# print an enum member as a string

print ( " The string representation of enum member is: " , end = "")

print (Animal.dog)

 
# print enum element as re pr

print ( "The repr representation of enum member is: " , end = " ")

print ( repr (Animal.dog))

 
# print enumeration member type using type ()

print ( " The type of enum member is: " , end = "")

print ( type (Animal.dog))

 
# print member name and enum using the name keyword

print ( " The name of enum member is: " , end = "")

print (Animal.dog.name)

Output:

 The string representation of enum member is: Animal.dog The repr representation of enum member is: "Animal.dog: 1" The type of enum member is: "enum ’Animal’" The name of enum member is: dog 

4. Enumerations are repeatable . They can be repeated using loops

5. Enumerations support hashing . Enumerations can be used in dictionaries or sets .

# Python code to demonstrate enums
# iteration and hashing
# enum import for enums

import enum

  
# create enums using class

class Animal (enum.Enum):

dog = 1

 < / code> cat = 2

lion = 3

 
# print all enum members using a loop

print ( "All the enum values ​​are:" )

for Anim in (Animal):

  print (Anim)

 
# Hashing enum member as a dictionary

di = {}

di [Animal.dog] = ’bark ’

di [Animal.lion] = ’roar’

  
# check if enumeration values ​​have been successfully hashed

if di = = {Animal.dog: ’bark’ , Animal.lion: ’roar’ }:

  print ( " Enum is hashed " )

else : print ( "Enum is not hashed" )

Output:

 All the enum values ​​are: Animal.dog Animal.cat Animal.lion Enum is hashed 

Access to modes: There are two ways to access Enum elements

1. By value : — This method is passed the value of an enumeration member.

2. By name : — This method passes the name of an enumeration member.

A single value or name can also be retrieved using the keyword " name " or " value ".

Comparison: Enumerations support two types of comparisons

1. Identity : — They are checked using the keywords " is " and " not ".

2. Equality : — Comparisons for equality of types " == " and "! = ".

# Python code to demonstrate enumerations
# Access and comparison

 
# enum import for enums

import enum

 
# create enums using the class

class Animal (enum.Enum):

dog = 1

  cat = 2

lio n = 3

  
# Access enum element using value

print ( " The enum member associated with value 2 is: " , end = " ")

print (Animal ( 2 ))

  
# Access enum element by name

print ( "The enum member associated with name lion is:" , end = "")

print (Anima l [ ’lion’ ])

  
# Enum member assignment

mem = Animal.dog

  
# Display value

print ( " The value associated with dog is: " , end = "")

print (mem.value)

 
# Display name

print ( "The name associated with dog is:" , end = "")

print (mem.name)

 
# Comparison using & quot; is & quot;

if Animal.dog is Animal.cat:

  print ( " Dog and cat are same animals " )

else : print ( "Dog and cat are different animals"

 
# Comparison using & quot ;! = & quot;

if Animal.lion! = Animal.cat:

print ( "Lions and cat are different" )

else : print ( "Lions and cat are same" )

Output:

 The enum member associated with value 2 is: Animal.cat The enum member associated with name lion is: Animal.lion The value associated with dog is: 1 The name associated with dog is: dog Dog and cat are different animals Lions and cat are different 

This article is courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically