Python enumeration [1], python Enumeration

Source: Internet
Author: User

Python enumeration [1], python Enumeration

1. Enumeration Definition

Sample Code:

from enum import Enum

class Color(Enum): red = 1 orange = 2 yellow = 3 green = 4 blue = 5 indigo = 6 purple = 7

Code Analysis:

1.1 duplicate Member names are not allowed when enumeration is defined.
from enum import Enum

class Color(Enum): red = 1 red = 2

The above Code cannot be executed. Error message: TypeError: Attempted to reuse key: 'red'

 1.2 By default, different Member values are allowed to be the same. However, for two members with the same value, the name of the second member is treated as the alias of the first member.  

from enum import Enum

class Color(Enum): red = 1 red_alias = 1

The Color. red and Color. red_alias members share the same value, so the name of the member Color. red_alias is treated as the alias of the member Color. red name red.

1.3 if a member with the same value exists in the enumeration, only the first member can be obtained when the enumerated member is obtained through the value.

from enum import Enum

class Color(Enum): red = 1 red_alias = 1print(Color(1))

Output result: Color. red

 1.4 if you want to restrict the definition of enumeration, you cannot define members with the same value. You can use the decorator @ unique to import the unique module]

from enum import Enum, unique@uniqueclass Color(Enum):    red = 1    red_alias = 1

If you run the command again, the error "ValueError: duplicate values found in <enum 'color'>: red_alias-> red" is displayed.

 

2. Enumeration values

2.1 get a member by the member name

Color['red']

 2.2 obtain members using the member Value

Color(2)

2.3 obtain the name and value of a member.

red_member = Color.redred_member.namered_member.value

 

3. iterator

3.1 enumeration supports the iterator to traverse enumeration members

for color in Color:    print(color)

The output result is all enumeration members.Color. red, Color. orange, Color. yellow, Color. green, Color. blue, Color. indigo, Color. purple.

 3.2 If a member with duplicate values is enumerated, only the first member with duplicate values is obtained during cyclic enumeration.

from enum import Enumclass Color(Enum):    red = 1    orange = 2    yellow = 3    green = 4    blue = 5    indigo = 6    purple = 7    red_alias = 1for color in Color:    print(color)

The output result is:Color. red, Color. orange, Color. yellow, Color. green, Color. blue, Color. indigo, Color. purple.HoweverColor. red_aliasIt does not appear in the output result.

 3.3 If You Want to traverse members with duplicate values, use a special attribute _ members _ of enumeration __

from enum import Enumclass Color(Enum):    red = 1    orange = 2    yellow = 3    green = 4    blue = 5    indigo = 6    purple = 7    red_alias = 1for color in Color.__members__.items():    print(color)

Output result: ('red', <Color. red: 1>), ('Orange ', <Color. orange: 2>), ('yellow', <Color. yellow: 3>), ('green', <Color. green: 4> ),('Blue', <Color. blue: 5> ),

       ('Indigo ', <Color. indigo: 6> ),('Purple ', <Color. purple: 7> ),('Red _ alias', <Color. red: 1>)

 

4. Enumeration comparison

4.1 enumeration members can perform the same identity comparison.

Color.red is Color.red

The output result is True.

Color.red is not Color.blue

The output result is True.

 4.2 enumeration members can be compared with equivalent values.

Color.blue == Color.red

The output result is: False.

Color.blue != Color.red

The output result is True.

 4.3 enumeration members cannot be compared in size.

Color.red < Color.blue

Output result error: TypeError: unorderable types: Color () <Color ()

 

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.