Life is short. Python enum type enum

Source: Internet
Author: User

Tag: Sep How ber mem val Class A blog python

Enum type Enum is an important data type, which is a data type rather than a structure, and we usually declare a common set of constants as enumerated types for easy follow-up use. When there are several possible values for a variable, we define it as an enumeration type. How is it implemented in Python?

Add: Many beginners suddenly do not understand what enumeration means, for example, we have a program in a lot of places need to use 12 months, you can define each month to use: String Jan = "January", but this is very bad use, need to write 12, and then the subsequent use of the time is not clear, The enumeration type can solve the problem, it lists the 12 months, and then each month corresponds to a value (from 0 to ...). ), we can use the declared enumeration type to bring out the values we want directly.

We declare the enumeration type for a month.

>>> from enum import enum>>> Month = enum (' Month ', [' Jan ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' Jul ', ' April ', ' Sep ', ' Oct ', ' Nov ', ' Dec ') >>> for Name,member in Month.__members__.items ():p rint (name, ' = = ', member, ', ',  Member.value) (' Jan ', ' = ', ' <month.jan:1> ', ', ', 1 ') (' Feb ', ' = ', ' <month.feb:2> ', ', ', ' 2 ') (' Mar ', ' = ', <month.mar:3>, ', ', 3 ' (' Apr ', ' = ', ' <month.apr:4> ', ', ', ' 4 ') (' may ', ' = = ', <month.may:5>, ', ', 5) (' June ', ' = = ', <month.jun:6>, ', ', 6) (' Jul ', ' = = ', <month.jul:7>, ', ', 7) (' <month.aug:8> ', ' = = ', ' n ', ', ', ', ' 8 ') (' Sep ', ' = = ', <month.sep:9>, ', ', 9) (' Oct ', ' = = ', <month.oct:10>, ', ', 10) (' Nov ', ' = = ', <month.nov:11>, ', ', 11) (' Dec ', ' = = ', <month.dec:12>, ', ', 12)

First, the enum module is imported, and then the enumeration type name and its possible values are declared. There is another way we define an enum subclass to define an enumeration class.

>>> from enum import enum,unique>>> @uniqueclass Weekday (enum): Sun = 0Mon = 1Tue = 2Wed = 3Thu = 4Fri = 5Sat = 6

@unique This decorator is to help us check if there are duplicate values. It is also a variety of values to take out enumerated types.

>>> Print (WEEKDAY.FRI)
Weekday.fri
>>> Print (Weekday (2))
Weekday.tue
>>> a = Weekday (2)
>>> Print a
Weekday.tue

As you can see from the last one, there is a difference between defining an enumeration class and defining an ordinary class, and at the beginning it says that the enumeration type is a set of constants, and we just want to make it easy for you to use a set of possible values in one place and take the values out as needed.

Life is short. Python enum type enum

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.