Python/python11/2.md

28 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 二、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__` 来进行访问了