Python, using enum classes, object-oriented advanced programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.