GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python_test/test25.py at master · sinferwu/python_test Expand file tree/ Copy pathtest25.pyMore file actions 61 lines (47 loc) · 1.37 KB / Copy pathtest25.pyFile metadata and controls61 lines (47 loc) · 1.37 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 #!/usr/bin/env python 3 # -*- coding: utf-8 -*- '练习面向对象的高级特性的枚举' __author__ = 'sergiojune' from enum import Enum, unique # 使用枚举 # Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) # 获取每一个成员,value是自动生成的,从 1 开始 for month,member in Month.__members__.items(): print(month, '=>', member, member.value) # 可以继承枚举来实现枚举类 @unique # 这个装饰器是让枚举的键固定值,并且不能相同 class Weekday(Enum): # 这样就实现了枚举类 Sun = 0 Mon = 1 Tue = 2 Wed = 3 Thu = 4 Fri = 5 Sat = 6 # 访问枚举类的值 print(Weekday.Sun) print(Weekday(1)) print(Weekday['Wed']) num =4 # 这个访问值 print(Weekday(4).value == num) # 这个访问键 print(Weekday(4) == num) for day, member in Weekday.__members__.items(): print(day, '=>', member) # 作业:把Student的gender属性改造为枚举类型,可以避免使用字符串 class Gender(Enum): Male = 0 Female = 1 class Student(object): def __init__(self, name, gender): self.name = name self.gender = gender # 测试: bart = Student('Bart', Gender.Male) if bart.gender == Gender.Male: print('测试通过!') else: print('测试失败!') |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |