This repository was archived by the owner on Dec 2, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +93
-0
lines changed Expand file tree Collapse file tree 1 file changed +93
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " markdown" ,
5
+ "metadata" : {},
6
+ "source" : [
7
+ " # Ch3 字典和集合\n " ,
8
+ " 本质是散列表\n " ,
9
+ " \n " ,
10
+ " ## 3.1 泛映射类型\n " ,
11
+ " \n " ,
12
+ " > collections.abc 模块中有Mapping 和 MutableMapping 这两个抽象基类,它们的作用是为字典和集合dict和其他类似的类型定义形式接口\n " ,
13
+ " \n " ,
14
+ " 继承树:\n " ,
15
+ " \n " ,
16
+ " `Container` class\n " ,
17
+ " - `__contains__`\n " ,
18
+ " \n " ,
19
+ " `Iterable` class\n " ,
20
+ " - `__iter__`\n " ,
21
+ " \n " ,
22
+ " `Sized` class\n " ,
23
+ " - `__len__`\n " ,
24
+ " \n " ,
25
+ " `Mapping` class extends `Container`, `Iterable`, `Sized`\n " ,
26
+ " - `__getitem__`\n " ,
27
+ " - `___contains__`\n " ,
28
+ " - `__eq__`\n " ,
29
+ " - `__ne__`\n " ,
30
+ " - `get`\n " ,
31
+ " - `keys`\n " ,
32
+ " - `items`\n " ,
33
+ " - `values`\n " ,
34
+ " \n " ,
35
+ " `MutableMapping` class extends `Mapping`\n " ,
36
+ " - `__setitem__`\n " ,
37
+ " - `__delitem__`\n " ,
38
+ " - `pop`\n " ,
39
+ " - `popitem`\n " ,
40
+ " - `clear`\n " ,
41
+ " - `update`\n " ,
42
+ " - `setdefault`\n " ,
43
+ " \n " ,
44
+ " > 然而,非抽象映射类型一般不会直接继承这些抽象基类,它们会直接对`dict`或是`collections.User.Dict`进行扩展。这些抽象基类的主要作用是作为形式化的文档,它们定义了构建一个映射类型所需要的最基本的接口。\n "
45
+ ]
46
+ },
47
+ {
48
+ "cell_type" : " code" ,
49
+ "execution_count" : 1 ,
50
+ "metadata" : {},
51
+ "outputs" : [
52
+ {
53
+ "data" : {
54
+ "text/plain" : [
55
+ " True"
56
+ ]
57
+ },
58
+ "execution_count" : 1 ,
59
+ "metadata" : {},
60
+ "output_type" : " execute_result"
61
+ }
62
+ ],
63
+ "source" : [
64
+ " import collections.abc as abc\n " ,
65
+ " \n " ,
66
+ " my_dict = {}\n " ,
67
+ " isinstance(my_dict, abc.Mapping)\n " ,
68
+ " #这里用isinstance而不是type来检查某个参数是否为dict类型,因为这个参数有可能不是dict,而是一个比较另类的映射类型。"
69
+ ]
70
+ }
71
+ ],
72
+ "metadata" : {
73
+ "kernelspec" : {
74
+ "display_name" : " base" ,
75
+ "language" : " python" ,
76
+ "name" : " python3"
77
+ },
78
+ "language_info" : {
79
+ "codemirror_mode" : {
80
+ "name" : " ipython" ,
81
+ "version" : 3
82
+ },
83
+ "file_extension" : " .py" ,
84
+ "mimetype" : " text/x-python" ,
85
+ "name" : " python" ,
86
+ "nbconvert_exporter" : " python" ,
87
+ "pygments_lexer" : " ipython3" ,
88
+ "version" : " 3.10.9"
89
+ }
90
+ },
91
+ "nbformat" : 4 ,
92
+ "nbformat_minor" : 2
93
+ }
You can’t perform that action at this time.
0 commit comments