mirror of https://github.com/TwoWater/Python
Signed-off-by: twowater <347073565@qq.com>
parent
245dd86f40
commit
62502132a6
|
@ -17,4 +17,5 @@
|
|||
|草根学Python(七) 迭代器和生成器|[掘金](https://juejin.im/post/59589fedf265da6c386ce4ac),[简书](http://www.jianshu.com/p/74c0c1db1490),[CSDN](http://blog.csdn.net/Two_Water/article/details/74164652),[个人博客](http://twowater.com.cn/2017/07/02/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B8%83-%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%92%8C%E7%94%9F%E6%88%90%E5%99%A8/)|
|
||||
|草根学Python(八) 模块与包|[掘金](https://juejin.im/post/5962ddf95188252ec34009da),[简书](http://www.jianshu.com/p/7f05f915d2ac),[CSDN](http://blog.csdn.net/Two_Water/article/details/75042211),[个人博客](http://twowater.com.cn/2017/07/12/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%85%AB-%E6%A8%A1%E5%9D%97%E4%B8%8E%E5%8C%85/)|
|
||||
|草根学Python(九) 面向对象|[掘金](https://juejin.im/post/596ca6656fb9a06b9b73c8b0),[简书](http://www.jianshu.com/p/6ecaa414c702),[CSDN](http://blog.csdn.net/two_water/article/details/76408890),[个人博客](http://twowater.com.cn/2017/07/31/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B9%9D-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/)|
|
||||
|草根学Python(十)Python 的 Magic Method|[掘金](https://juejin.im/post/59828c2f6fb9a03c56319baa),[简书](http://www.jianshu.com/p/345a80a02546),[CSDN](http://blog.csdn.net/two_water/article/details/77351516),[个人博客](http://twowater.com.cn/2017/08/18/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%8D%81-Python-%E7%9A%84-Magic-Method/)|
|
||||
|草根学Python(十)Python 的 Magic Method|[掘金](https://juejin.im/post/59828c2f6fb9a03c56319baa),[简书](http://www.jianshu.com/p/345a80a02546),[CSDN](http://blog.csdn.net/two_water/article/details/77351516),[个人博客](http://twowater.com.cn/2017/08/18/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%8D%81-Python-%E7%9A%84-Magic-Method/)|
|
||||
|草根学Python(十)Python 的 Magic Method|[掘金](https://juejin.im/post/599906a2f265da24722faec3),[简书](http://www.jianshu.com/p/c4d2629e5015),[CSDN](http://blog.csdn.net/Two_Water/article/details/77626179),[个人博客](http://twowater.com.cn/2017/08/28/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%8D%81%E4%B8%80-%E6%9E%9A%E4%B8%BE%E7%B1%BB/)|
|
|
@ -56,4 +56,9 @@
|
|||
- [属性的访问控制](/python10/3.md)
|
||||
- [对象的描述器](/python10/4.md)
|
||||
- [自定义容器(Container)](/python10/5.md)
|
||||
- [运算符相关的魔术方法](/python10/6.md)
|
||||
- [运算符相关的魔术方法](/python10/6.md)
|
||||
* [草根学Python(十一)枚举类](/python11/Preface.md)
|
||||
- [枚举类的使用](/python11/1.md)
|
||||
- [Enum 的源码](/python11/2.md)
|
||||
- [自定义类型的枚举](/python11/3.md)
|
||||
- [枚举的比较](/python11/4.md)
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
# 一、枚举类的使用 #
|
||||
|
||||
实际开发中,我们离不开定义常量,当我们需要定义常量时,其中一个办法是用大写变量通过整数来定义,例如月份:
|
||||
|
||||
```python
|
||||
JAN = 1
|
||||
FEB = 2
|
||||
MAR = 3
|
||||
...
|
||||
NOV = 11
|
||||
DEC = 12
|
||||
```
|
||||
|
||||
当然这样做简单快捷,缺点是类型是 `int` ,并且仍然是变量。
|
||||
|
||||
那有没有什么好的方法呢?
|
||||
|
||||
这时候我们定义一个 class 类型,每个常量都是 class 里面唯一的实例。正好 Python 提供了 Enum 类来实现这个功能如下:
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
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(name, '---------', member, '----------', member.value)
|
||||
|
||||
# 直接引用一个常量
|
||||
print('\n', Month.Jan)
|
||||
|
||||
```
|
||||
|
||||
输出的结果如下:
|
||||
|
||||
|
||||

|
||||
|
||||
可见,我们可以直接使用 `Enum` 来定义一个枚举类。上面的代码,我们创建了一个有关月份的枚举类型 Month ,这里要注意的是构造参数,第一个参数 Month 表示的是该枚举类的类名,第二个 tuple 参数,表示的是枚举类的值;当然,枚举类通过 `__members__` 遍历它的所有成员的方法。注意的一点是 , `member.value` 是自动赋给成员的 `int`类型的常量,默认是从 1 开始的。而且 Enum 的成员均为单例(Singleton),并且不可实例化,不可更改
|
|
@ -0,0 +1,28 @@
|
|||
# 二、Enum 的源码 #
|
||||
|
||||
通过上面的实例可以知道通过 `__members__` 可以遍历枚举类的所有成员。那为什么呢?
|
||||
|
||||
我们可以先来大致看看 Enum 的源码是如何实现的;Enum 在模块 enum.py 中,先来看看 Enum 类的片段
|
||||
|
||||
```python
|
||||
class Enum(metaclass=EnumMeta):
|
||||
"""Generic enumeration.
|
||||
Derive from this class to define new enumerations.
|
||||
"""
|
||||
```
|
||||
|
||||
可以看到,Enum 是继承元类 EnumMeta 的;再看看 EnumMeta 的相关片段
|
||||
|
||||
```python
|
||||
class EnumMeta(type):
|
||||
"""Metaclass for Enum"""
|
||||
@property
|
||||
def __members__(cls):
|
||||
"""Returns a mapping of member name->value.
|
||||
This mapping lists all enum members, including aliases. Note that this
|
||||
is a read-only view of the internal mapping.
|
||||
"""
|
||||
return MappingProxyType(cls._member_map_)
|
||||
```
|
||||
|
||||
首先 `__members__` 方法返回的是一个包含一个 Dict 既 Map 的 MappingProxyType,并且通过 @property 将方法 `__members__(cls)` 的访问方式改变为了变量的的形式,既可以直接通过 `__members__` 来进行访问了
|
|
@ -0,0 +1,45 @@
|
|||
# 三、自定义类型的枚举 #
|
||||
|
||||
但有些时候我们需要控制枚举的类型,那么我们可以 Enum 派生出自定义类来满足这种需要。通过修改上面的例子:
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
from enum import Enum, unique
|
||||
|
||||
Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
|
||||
|
||||
|
||||
# @unique 装饰器可以帮助我们检查保证没有重复值
|
||||
@unique
|
||||
class Month(Enum):
|
||||
Jan = 'January'
|
||||
Feb = 'February'
|
||||
Mar = 'March'
|
||||
Apr = 'April'
|
||||
May = 'May'
|
||||
Jun = 'June'
|
||||
Jul = 'July'
|
||||
Aug = 'August'
|
||||
Sep = 'September '
|
||||
Oct = 'October'
|
||||
Nov = 'November'
|
||||
Dec = 'December'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(Month.Jan, '----------',
|
||||
Month.Jan.name, '----------', Month.Jan.value)
|
||||
for name, member in Month.__members__.items():
|
||||
print(name, '----------', member, '----------', member.value)
|
||||
|
||||
```
|
||||
|
||||
|
||||
输出的结果如下:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
通过上面的例子,可以知道枚举模块定义了具有迭代 (interator) 和比较(comparison) 功能的枚举类型。 它可以用来为值创建明确定义的符号,而不是使用具体的整数或字符串。
|
|
@ -0,0 +1,75 @@
|
|||
# 四、枚举的比较 #
|
||||
|
||||
因为枚举成员不是有序的,所以它们只支持通过标识(identity) 和相等性 (equality) 进行比较。下面来看看 `==` 和 `is` 的使用:
|
||||
|
||||
```python
|
||||
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class User(Enum):
|
||||
Twowater = 98
|
||||
Liangdianshui = 30
|
||||
Tom = 12
|
||||
|
||||
|
||||
Twowater = User.Twowater
|
||||
Liangdianshui = User.Liangdianshui
|
||||
|
||||
print(Twowater == Liangdianshui, Twowater == User.Twowater)
|
||||
print(Twowater is Liangdianshui, Twowater is User.Twowater)
|
||||
|
||||
try:
|
||||
print('\n'.join(' ' + s.name for s in sorted(User)))
|
||||
except TypeError as err:
|
||||
print(' Error : {}'.format(err))
|
||||
|
||||
```
|
||||
|
||||
输出的结果:
|
||||
|
||||
```txt
|
||||
|
||||
False True
|
||||
False True
|
||||
Error : '<' not supported between instances of 'User' and 'User'
|
||||
|
||||
```
|
||||
|
||||
可以看看最后的输出结果,报了个异常,那是因为大于和小于比较运算符引发 TypeError 异常。也就是 `Enum` 类的枚举是不支持大小运算符的比较的。
|
||||
|
||||
那么能不能让枚举类进行大小的比较呢?
|
||||
|
||||
当然是可以的,使用 IntEnum 类进行枚举,就支持比较功能。
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import enum
|
||||
|
||||
|
||||
class User(enum.IntEnum):
|
||||
Twowater = 98
|
||||
Liangdianshui = 30
|
||||
Tom = 12
|
||||
|
||||
|
||||
try:
|
||||
print('\n'.join(s.name for s in sorted(User)))
|
||||
except TypeError as err:
|
||||
print(' Error : {}'.format(err))
|
||||
|
||||
|
||||
```
|
||||
|
||||
看看输出的结果:
|
||||
|
||||
```txt
|
||||
Tom
|
||||
Liangdianshui
|
||||
Twowater
|
||||
```
|
||||
|
||||
通过输出的结果可以看到,枚举类的成员通过其值得大小进行了排序。也就是说可以进行大小的比较。
|
|
@ -0,0 +1,9 @@
|
|||
# 前言 #
|
||||
|
||||
虽然没多少阅读,可是还是坚持写下去。对 Python 感兴趣的童鞋可以加入 Python 学习讨论微信群喔。可以先加我微信,然后拉进群。本人微信:
|
||||
|
||||

|
||||
|
||||
# 目录 #
|
||||
|
||||

|
Loading…
Reference in New Issue