Python
#for Constants#An enumeration type defines a class type, and then each constant is a unique instance of class. fromEnumImportEnummonth= Enum ('Month', ('Jan','Feb','Mar','APR',' May','June','Jul',' the','Sep','Oct','Nov','Dec'))#so we get an enumeration class of type month . forName, memberinchMonth.__members__. Items ():Print(Name,'=', member,',', Member.value)#The Value property is an int constant that is automatically assigned to a member, and is counted by default starting at 1.
If you need more precise control over the enumeration type, you can derive the custom class from the enum:
fromEnumImportEnum, Unique@uniqueclassWeekday (Enum): Sun= 0#Sun's value is set to 0Mon = 1Tue= 2Wed= 3Thu= 4Fri= 5Sat= 6#@unique adorners can help us check that there are no duplicate values. #There are several ways to access these enumeration types:Day1=Weekday.monPrint(day1) Weekday.monPrint(weekday['Tue']) Weekday.tuePrint(Weekday.Tue.value) 2Print(Weekday (1)) Weekday.monPrint(Day1 = = Weekday (1)) True forName, memberinchWeekday.__members__. Items ():Print(Name,'=', member)#ResultsSun =Weekday.sunmon=Weekday.montue=weekday.tuewed=Weekday.wedthu=Weekday.thufri=Weekday.frisat= Weekday.sat
Python, using enum classes, object-oriented advanced programming