GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com

# 二、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__` 来进行访问了
You are visitor 01402792 — 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.