python enumeration type

Notes:


I: definition of enumeration type
The difference between enumeration class and common class
1: value is immutable
2: it has the function of preventing the same label. The values of different labels can be the same!


III: enumeration type, enumeration value and enumeration name
1: VIP.YELLOW.Value get the value of the tag
2: VIP.BLACK.name get label name
3: part of enumeration is of enumeration type!

Four: comparison between enumerations
1: support equivalent comparison and is, not size comparison
2: comparison between different enumeration classes

V. precautions for enumeration
When the values of different tags are the same, the second one will be used as an alias and will be overwritten when calling!
When traversing, it will not be printed out. The solution is:
for v in VIP.__members__.items():
print(v)

VI: enumeration conversion
Remove the value corresponding to a value from the database (a = 1), and match the enumeration type according to the value of a
a = 1
print(VIP(a))

VII. Others
1: IntEnum Type Description: all enumerations are of int type!
2: the value of restricted enumeration type cannot be the same

-------23 design modes --- learning in practice--------
-------Try to write some packages and class libraries------------------
CODE:
  1 # ----------------------------------------------#
  2 # Define an enumeration type
  3 # ----------------------------------------------#
  4 
  5 from enum import Enum
  6 from enum import IntEnum, unique
  7 
  8 
  9 class VIP(Enum):
 10     YELLOW = 1
 11     GREEN = 2
 12     BLACK = 3
 13     RED = 4
 14 
 15 
 16 class Common:
 17     YELLOW = 2
 18 
 19 
 20 # Notice what's shown here VIP.Black Instead of 3, enumerations are more about tags than their values
 21 print(VIP.BLACK)
 22 
 23 
 24 # ----------------------------------------------------#
 25 # The difference between enumeration class and common class
 26 # Dictionary and common class encapsulate data: 1: variable    2: no function to prevent the same label
 27 # ----------------------------------------------------#
 28 
 29 # VIP.GREEN = 6     # Report a mistake when the change is worth it
 30 
 31 Common.YELLOW = 9   # Change the value of a class variable
 32 print(Common.YELLOW)
 33 x = Common()           # Class instantiation
 34 print(x.YELLOW)
 35 
 36 # ----------------------------------------------#
 37 # Enum type, enum value, enum name
 38 # ----------------------------------------------#
 39 print("Black = ", VIP.BLACK.value)  # get value of Enum
 40 
 41 print("name = ", VIP.BLACK.name)  # get value of Nameļ¼Œthe type of name is str
 42 
 43 print(VIP.BLACK)        # Name is enumeration type
 44 
 45 print(VIP['BLACK'])
 46 
 47 # Enumeration can be traversed
 48 for v in VIP:
 49     print(v)
 50 
 51 
 52 # ----------------------------------------------#
 53 # Comparison between enumerations,Support equivalent comparison, not size comparison
 54 # ----------------------------------------------#
 55 
 56 result1 = VIP.GREEN == VIP.BLACK
 57 result2 = VIP.GREEN.value == 2    # Pay attention to the results here
 58 print(result1, result2)
 59 
 60 
 61 class VIP1(Enum):
 62     YELLOW = 1
 63     GREEN = 2
 64     BLACK = 3
 65     RED = 4
 66     WRITE = 5
 67 
 68 
 69 result3 = VIP.GREEN == VIP1.GREEN
 70 print(result3)
 71 
 72 # ----------------------------------------------#
 73 # Enumeration considerations
 74 # ----------------------------------------------#
 75 
 76 
 77 class VIP2(Enum):
 78     YELLOW = 1
 79     GREEN = 1   # alias
 80     BLACK = 3
 81     RED = 4
 82 
 83 
 84 for v in VIP2:
 85     print(v)
 86 
 87 for v in VIP2.__members__.items():
 88     print(v)
 89 
 90 for v in VIP2.__members__:
 91     print(v)
 92 
 93 # ----------------------------------------------#
 94 # Enumeration conversion
 95 # ----------------------------------------------#
 96 
 97 a = 1
 98 print(VIP(a))       # It can be seen as a type conversion
 99 
100 # ----------------------------------------------#
101 # Enumeration supplement
102 # ----------------------------------------------#
103 
104 # 1:IntEnum Type description
105 
106 
107 class VIP3(Enum):
108     YELLOW = 1
109     GREEN = "str"   # alias
110     BLACK = 3
111     RED = 4
112 
113 
114 class VIP4(IntEnum):
115     YELLOW = 1
116     # GREEN = 'str'   # String error
117     BLACK = 3
118     RED = 4
119 
120 
121 # 2:The value of restricted enumeration type cannot be the same
122 @unique
123 class VIP6(Enum):
124     YELLOW = 1
125     GREEN = 1   # Error will be reported when the value is the same!
126     BLACK = 3
127     RED = 4

Keywords: Python Database

Added by jigen7 on Tue, 17 Dec 2019 23:55:40 +0200