Python--enum

Source: Internet
Author: User

# Enum is used for enumeration, there is an enum under the module, we define the class to inherit it # once inherited, then we define the key (MUFANWU), cannot have duplicate values. # If you want to ensure that value (MUFANWU) is not duplicated, then introduce a unique, add this adorner to the class we define from the enum import enum, Unique@uniqueclass Color (enum):    red = 1    Green = 2    blue = 3    yellow = 4    pink = 5    cyan = 6# gets a member by name, two ways print (color["Red"])  # color.redprint (Col or.red)  # color.red# by value to get member print (Color (2))  # color.green# Gets the name and value of the member print (Color.red.name)  # redprint ( Color.red.value)  # # can also get members by iteration for the color in color:    print (Color.name, color.value) ' Red 1green 2blue 3yellow 4pink 5cyan 6 "

  

# If you do not add a unique, it will allow the same value. From enum import Enumclass Girls (enum): Satori = 1 Mashiro = 2 Nagisa = 3 Tomoyo = 4 Kurisu = 5 Sola = # Satori and sola values are 1print (Girls (1)) # Girls.satori, you will find that only satori is printed, that is, the first one. If the value is the same, then the latter is equivalent to the former alias. # traversal, will find Sola also not be printed out for girl in Girls:print (girl) " Girls.satoriGirls.satoriGirls.mashiroGirls.nagisaGirls.tomoyoGirls.kurisu ' # What if you want to get all the members, even if the values are the same? # Enum Property has a __members__ property, equivalent to a dictionary for Girl in Girls.__members__.items (): Print (Girl) "' (' Satori ', <girls.satori:1& gt;) (' Mashiro ', <Girls.mashiro:2>) (' Nagisa ', <Girls.nagisa:3>) (' Tomoyo ', <Girls.tomoyo:4>) (' Kurisu ', <Girls.kurisu:5>) (' Sola ', <Girls.satori:1>) ' # will be found in a more detailed way to print out # and the second element of each tuple is <enum ' Girls ' > Type # Don't be fooled by <>, actually we use Girls.satori,girls.mashiro Get results for girl in Girls.__members__.items (): Print (Girl[1].name, girl[1].value) ' Satori 1mashiro 2nagisa 3tomoyo 4kur ISU 5satori 1 "# members can also be compared, but only with ==,is and other operators, do not support size comparison print (GIRLS.SAtori is Girls.mashiro) # falseprint (Girls.satori = Girls.mashiro) # Falseprint (Girls.satori is Girls.sola) # True, because two Value, the latter is the alias of the former, two pointers to the same place print (Girls.satori is Girls.sola) # true# If you change 1 to a list, class Girls (Enum): Satori = [1, 2] Ma  Shiro = 2 Nagisa = 3 Tomoyo = 4 Kurisu = 5 Sola = [1, 2] Kurumi = [1, 2]print (Girls.satori is Girls.sola) # Trueprint (Girls.satori is Girls.sola) # Trueprint (Girls.kurumi is Girls.sola) # true# will find still True, although the list is mutable type, but if the value is the same, it will continue to let Sola pointer to satori Pointer to memory # because of the same value, then sola as a Satori Reference # same as a Kurumi is the same result # without __members__, the print is still the first Satori

  

Python--enum

Related Article

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.