码迷,mamicode.com
首页 > 编程语言 > 详细

Python -- OOP高级 -- 枚举类

时间:2015-10-22 12:30:51      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:


 

Enum可以把一组相关常量定义在一个class中,且class不可变,而且成员可以直接比较。

from enum import Enum

Month = Enum(Month, (Jan, Feb, Mar, Apr, May, Jun,                         Jul, Aug, Sep, Oct, Nov, Dec))
                        
for name, member in Month.__members__.items():
    print("%s => %s, %d" % (name, member, member.value))

>>> 
Jan => Month.Jan, 1
Feb => Month.Feb, 2
Mar => Month.Mar, 3
Apr => Month.Apr, 4
May => Month.May, 5
Jun => Month.Jun, 6
Jul => Month.Jul, 7
Aug => Month.Aug, 8
Sep => Month.Sep, 9
Oct => Month.Oct, 10
Nov => Month.Nov, 11
Dec => Month.Dec, 12

 


 

Python -- OOP高级 -- 枚举类

标签:

原文地址:http://www.cnblogs.com/roronoa-sqd/p/4900427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!