Python Class: Object-oriented Advanced programming enum (enum), @unique

Source: Internet
Author: User

Note: Python version 3.x can be used

Enumeration format:

from enum import enum

New Class name = Enum (variable name, variable 1, variable 2 ...)

month = Enum (' Month ', (' Jan ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' Jul ', ' April ', ' Sep ', ' Oct ', ' Nov ', ' Dec '))


1.

Seasons = [' Spring ', ' Summer ', ' Fall ', ' Winter ']print (List (enumerate (seasons)))

Operation Result:

[(0, ' Spring '), (1, ' Summer '), (2, ' Fall '), (3, ' Winter ')]


2.

from enum Import enummonth = enum (' Month ', (' Jan ', ' Feb ', ' Mar ', ' April ', ' may ', ' June ', ' Jul ', ' April ', ' Sep ', ' Oct ', ' Nov ', ' Dec ')) for name, member in Month.__members__.items (): Print (name, ' = = ', member, ', ', Member.value)

Operation Result:

Jan = month. Jan, 1Feb = month. Feb, 2Mar = month. Mar, 3APR = month. APR, 4May = month. May, 5Jun = month. June, 6Jul = month. Jul, 7Aug = month. 8SEP = month. SEP, 9Oct = month. OCT, 10Nov = month. Nov, 11Dec = month. DEC, 12


3. When there are duplicate values in class, the first one is returned, and the other ignores

From enum import Enumclass Weekday (enum): Monday = 1 Tusday = 1 Wensdday =3 Thursday =9 Friday =5#print (We Ekday (1)) for n in Weekday:print (n)

Operation Result:

Weekday.mondayWeekday.wensddayWeekday.thursdayWeekday.friday

Manually hit the enum value, accidentally hit the wrong how to do, and have to find, really trouble. So, some people think, if there is a way to check the duplicate value is good, so: @unique came out

From enum import enum, @uniqueclass Weekday (enum): Monday = 1 Tusday = 1 Wensdday =3 Thursday =9 Friday =5p Rint (Weekday (1)

Operation Result:

Traceback (most recent): File "/usercode/file.py", line 7, in <module> class Weekday (Enum): File "/us r/lib/python3.4/enum.py ", line 524, in unique (enumeration, alias_details))


4. Some people are also thinking, I just do not change the ground! Can I print out the members in another way? So: __members__ is out.

from enum import enumclass weekday (enum):     monday = 1     tusday = 1    wensdday = 3     thursday = 9    friday = 5for n,y in weekday.__ Members__.items ():    print  (n,  '---', y,  '--',   y.value)     f = Weekday.thursdayprint  (F.value) 

Operation Result:

Monday--weekday.monday--1tusday--weekday.tusday--1wensdday--weekday.wensdday--3thursday--weekday.thursday-- 9friday--Weekday.friday--59

5. Enumeration comparison: No bigger than size!! Can be compared to the same value

From enum import enum, unique# @uniqueclass Weekday (enum): Monday = 1 Tusday = 1 wensdday =3 Thursday =9 Fri Day =5print (Weekday.monday = = weekday.wensdday) print (Weekday.tusday is weekday.friday) print (Weekday.tusday > WEEKD Ay.friday)

Operation Result:

Falsefalsetraceback (most recent): File "/usercode/file.py", line <module> print (WEEKDAY.TUSDA Y > Weekday.friday) typeerror:unorderable types:weekday () > Weekday ()



See a good good blog, this talk about the content, he is more concise than Liao. I like it very much. How's My mess? I was purely a note-taking, not thinking so much, to change the structure to think more instead of wasting time.

66476601

Python Class: Object-oriented Advanced programming enum (enum), @unique

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.