Details of Python 3 enumeration

Posted by vincent30000 on Mon, 10 Feb 2020 17:39:51 +0100

1, Enumeration is actually a class

It is recommended that the identification name be capitalized

1. Enumeration class:

from enum import Enum
 
 
class VIP(Enum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4
 
 
print(VIP.YELLOW)
 
#VIP.YELLOW    #The concern is that its label is not a number

2 advantages of enumeration compared with common class

There are three other common ways to express enumeration:

(1)

yellow = 1

green = 2

(2)Dictionary representation

{'yellow':1,'green':2}    

(3)Class representation

class TypeDiamond():

    yellow = 1

    green = 2

These methods, which are all variable, can easily change the value in the code, and have no function to prevent the same label.

 

3. Enumeration features:

from enum import Enum
 
class VIP(Enum):
 
    YELLOW = 1
 
   #YELLOW= 2   #Non repeatable, error reported
 
    BLACK = 3
 
    RED = 4
 
print(VIP.YELLOW)
 
#VIP.YELLOW = 6    #Cannot be changed, error reported

2, Enumeration type, enumeration name and enumeration value

1. Get a value under enumeration type:

from enum import Enum
 
class VIP(Enum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4
 
    
 
print(VIP.YELLOW.value)
 
print(VIP.YELLOW.name)
 
print(VIP.YELLOW)
 
print(VIP['YELLOW'])
 
#1        #enum
 
#YELLOW    #< class' STR '> get tag name
 
#VIP.YELLOW    #< enum 'VIP' > enumeration type
 
#VIP.YELLOW    

2. Enumeration can be traversed:

from enum import Enum
 
 
class VIP(Enum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4
 
for v in VIP :
 
    print(v)
 
#VIP.YELLOW
VIP.GREEN
VIP.BLACK
VIP.RED

3, Comparison operation of enumeration

1. Equivalent comparison (= =) can be used between two enumerations. The result of enumeration and numerical comparison is wrong. There is no size comparison between two enumerations.

2. Support is operation:

result = VIP.GREEN is VIP.GREEN
 
#True

3. Equivalence comparison can also be made between two major classes, but the result is False:

from enum import Enum
 
class VIP(Enum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4
 
class VIP1(Enum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4
 
print(VIP.GREEN ==VIP1.GREEN)
 
#False

4, Enumeration considerations

1. Enumeration values can be the same. In this case, consider the second enumeration type as an alias. Aliases are not printed when traversing:

class VIP(Enum):
 
    YELLOW = 1
 
    GREEN = 1    #Alias, no error
 
    BLACK = 3
 
    RED = 4
 
print(VIP.GREEN)
 
#VIP.YELLOW

2. Add alias to traversal loop:

(1)
for v in VIP.__members__.items() :
 
    print(v)
 
#('YELLOW', <VIP.YELLOW: 1>)
 
#('GREEN', <VIP.GREEN: 1>)
 
#('BLACK', <VIP.BLACK: 3>)
 
#('RED', <VIP.RED: 4>)
 
(2)
 
//Or traverse members:
 
for v in VIP.__members__:
 
    print(v)
 
#YELLOW
 
#GREEN
 
#BLACK
 
#RED

5, Enumeration conversion

1. Generally store values or tag names in the database to represent enumeration types. It is recommended to store values. Numbers occupy less space. However, it is not recommended to use numerical value to represent enumeration in code, which is not readable.

2. How to convert a number to an enumeration type:

from enum import Enum
 
a = 1
 
class VIP(Enum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4
 
print(VIP(a))    #Convert enumeration type
 
#VIP.YELLOW

6, Enumeration summary

1. Inherit IntEnum when each enumeration type is a number:

from enum import IntEnum
 
class VIP(IntEnum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4

2. Limit different enumeration types to take the same value:

from enum import Enum
 
from enum import IntEnum,unique
 
@unique    #Decorator
 
class VIP(IntEnum):
 
    YELLOW = 1
 
    GREEN = 2
 
    BLACK = 3
 
    RED = 4

Enumeration type cannot be instantiated. It belongs to singleton mode

Topics: Python Database less