Python Learning note __7.5 using enum classes

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1. Overview

when we need to define regular constants, such as the month. We can use the Enum class to implement this function

1.1. Direct use of enum class

from enum import enum

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

>>> type (Month)

<class ' enum. Enummeta ' >

defines an enumeration type that is a Month class, where each constant is Month a unique instance of the class.

# Enumerate all members of month

For name, member in Month.__members__.items ():

print (name, ' = = ', member, ', ', Member.value)

Jan = Month.jan, 1

Feb = Month.feb, 2

...

Parametric analysis :

Each constant name in the Name:month

Each instance in the Member:month

The Member.value:value property is an int constant that is automatically assigned to a member, and is counted by default starting at 1

# Use

>>> Month.jan # directly referencing a constant, such as using the Month.jan

<Month.Jan:1>

>>> Month (1) # through value value to display

<Month.Jan:1>

# Look at the internal structure of month

>>> List (Month)  # we can see that Month internally, each instance and value a key-value pair is formed

[<month.jan:1>, <month.feb:2>, <month.mar:3>, <month.apr:4>, <month.may:5>,

<month.jun:6>, <month.jul:7>, <month.aug:8>, <month.sep:9>, <Month.Oct:10>

, <month.nov:11>, <month.dec:12>]

Note: The key value pair in month is determined by the enum (' Month ', (' Jan ',...) In the Month,jane,value, combined together. For example

>>> gender=enum ('Gender', (' Male ', ' Female '))

>>> List (Gender)

[<gender. Male:1>, <gender. Female:2>]

1.2. Deriving a custom class from an enum

If you need to control the enumeration type more precisely, you can Enum derived from the definition class using the

From enum import enum, unique

@unique # @unique adorners can help us check that there are no duplicate values

Class Weekday (Enum):

Sun = 0 # Custom Value value

Mon = 1

Tue = 2

Wed = 3

Thu = 4

Fri = 5

Sat = 6

Parametric analysis :

Constant name: Sun,mon, ...

Example: Weekday.sun,weekday.mon, ...

Value: 0,1,2, ...

# Structure of the weekday

>>> List (Weekday)

[<weekday.sun:0>, <weekday.mon:1>, <weekday.tue:2>, <weekday.wed:3>, <Weekda

Y.thu:4>, <weekday.fri:5>, <weekday.sat:6>]

# Differences between print (instance) and direct use instances

>>> Print (weekday.tue) # print, show only the instance name

Weekday.tue

>>> weekday.tue # Direct use of instances, display of key-value pairs

<Weekday.Tue:2>

>>> Print (Weekday (1))

Weekday.mon

>>> Weekday (1)

<Weekday.Mon:1>

1.3. Summary

enumeration constants can be referenced by a member name. Month.jan ", you can also get the enumeration constants directly based on value values" Month (1) "

2. Example

1 , the Student of the Gender property to an enumeration type, you can avoid using strings:

#-*-Coding:utf-8-*-

From enum import enum, unique

Method One: Use derived classes of enum

Class Gender (Enum):

Male = 0

Female = 1

Method Two: Use enum directly

Gender = Enum (' Gender ', (' Male ', ' Female '))

Class Student (object):

def __init__ (self, Name, gender):

Self.name = Name

Self.gender = Gender

Test
bart = Student (' Bart ', Gender.male)
if Bart.gender = = Gender.male:
Print (' Test pass! ')
Else
Print (' Test failed! ')


Python Learning note __7.5 using enum classes

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.