Python学习札记(三十九) 面向对象编程 Object Oriented Program 10
参考:使用枚举类
NOTE
#!/usr/bin/env python3
from enum import Enum
def main():
Mouth = Enum('Mouth', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
for name, attr in Mouth.__members__.items():
print(name, '=>', attr, '=>', attr.value)
if __name__ == '__main__':
main()
sh-3.2# ./oop11.py
Jan => Mouth.Jan => 1
Feb => Mouth.Feb => 2
Mar => Mouth.Mar => 3
Apr => Mouth.Apr => 4
May => Mouth.May => 5
Jun => Mouth.Jun => 6
Jul => Mouth.Jul => 7
Aug => Mouth.Aug => 8
Sep => Mouth.Sep => 9
Oct => Mouth.Oct => 10
Nov => Mouth.Nov => 11
Dec => Mouth.Dec => 12
1.当我们定义常量的时候,一般使用大写的字母来表示,但是本质上还是需要修改的。
更好的方法是为这样的枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例。Python提供了Enum类来实现这个功能。
枚举类Mouth有12个成员,每个成员默认有value属性,value属性默认从1开始分配。
2.也可以通过类的继承更好的控制枚举类:
from enum import Enum, unique
@unique
class Week(Enum):
"""docstring for Week"""
Sun = 0
Mon = 1
Tue = 2
Thu = 3
Wed = 4
Fri = 5
Sat = 6
print(Week.Sun)
print(Week['Tue'])
print(Week.Fri.value)
Week.Sun
Week.Tue
5
a.用成员名称引用枚举常量
b.根据value的值获得枚举常量
2017/3/7
Python学习札记(三十九) 面向对象编程 Object Oriented Program 10的更多相关文章
- Python学习札记(三十四) 面向对象编程 Object Oriented Program 5
参考:获取对象信息 NOTE 1.type()函数可以用来判断对象的类型: >>> type(123) <class 'int'> >>> type(' ...
- Python学习札记(三十八) 面向对象编程 Object Oriented Program 9
参考:多重继承 NOTE #!/usr/bin/env python3 class Animal(object): def __init__(self, name): self.name = name ...
- Python学习札记(三十六) 面向对象编程 Object Oriented Program 7 __slots__
参考:slots NOTE 1.动态语言灵活绑定属性及方法. #!/usr/bin/env python3 class MyClass(object): def __init__(self): pas ...
- Python学习札记(三十五) 面向对象编程 Object Oriented Program 6
参考:实例属性和类属性 NOTE Python是动态语言,根据类创建的实例可以任意绑定属性. class Student(object): def __init__(self, name): self ...
- Python学习札记(三十二) 面向对象编程 Object Oriented Program 3
参考:访问限制 NOTE 1.eg. #!/usr/bin/env python3 class Student(object): """docstring for Stu ...
- Python学习札记(三十) 面向对象编程 Object Oriented Program 1
参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...
- Python学习札记(四十) 面向对象编程 Object Oriented Program 11
参考:使用元类 NOTE: type() 1.type()函数可以用于检查一个类或者变量的类型. #!/usr/bin/env python3 class Myclass(object): " ...
- Python学习札记(三十七) 面向对象编程 Object Oriented Program 8 @property
参考:@property NOTE 1.在绑定参数时,为了避免对属性不符合逻辑的操作,需要对传入的参数进行审核. #!/usr/bin/env python3 class MyClass(object ...
- Python学习札记(三十三) 面向对象编程 Object Oriented Program 4
参考:继承和多态 NOTE 著名的开闭原则: 对扩展开放:允许新增Animal子类: 对修改封闭:不需要修改依赖Animal类型的Animal_func()等函数. 1.eg. #!/usr/bin/ ...
随机推荐
- CVE-2017-1000117命令注入验证
首先,我们来看第一个问题,就是ssh的一种操作. ssh -oProxyCommand=gnome-calculator xxx 问题的本质在于ssh会把//后的host那么部分带-号的按照参数指令去 ...
- 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树
[BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...
- html5新属性contenteditable 对于那些不可编辑的标签,现在都可以编辑了
contenteditable = true 表示该html标签的内容可以编辑,对于那些不可编辑的标签,现在都可以编辑了.
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence
使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character ' ...
- has to be escaped using backslash to be included in string value\n
[root@d myssh]# cat ESdel_bulk_file1544528090.log{"error":{"root_cause":[{" ...
- Create an Index
db.collection.createIndex( { name: -1 } ) Indexes — MongoDB Manual https://docs.mongodb.com/manual/i ...
- HTTP监视器charles入门使用教程分享---http/s packet monitors---ubuntu installation
charles --usage http://www.cnblogs.com/chenlogin/p/5849471.html 按照Charles的提示,PC打开 chls.pro/ssl下载得到一个 ...
- GraphicsMagick 号称图像处理领域的瑞士军刀
标签: librarydelegatesimage图像处理fontstiff 2012-09-13 10:15 2496人阅读 评论(0) 收藏 举报 分类: java技术(52) 简介 ...
- Centos 系统Java环境安装
Java安装 安装SUN的JDK: 官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260. ...
- redhat 7.2 内网安装docker
本文介绍在内网环境下如果通过网络代理映射来完成docekr的安装,首先在能上网的windows机器上安装squid,并启动,本实例中windows机器IP为 192.168.192.101 ,squi ...