Week6-Lecture13-15
Lecture 13 Data Abstraction¶
Reading 2.2 Data Abstraction¶
Compound Date的使用能让我们提高程序的模块化程度。
The general technique of isolating the parts of a program that deal with how data are represented from the parts that deal with how data are manipulated is a powerful design methodology called data abstraction.
将数据的表示和计算方法分离开,这种方法被称之为数据抽象。
2.2.1 Example: Rational Numbers¶
| Python | |
|---|---|
2.2.2 Pairs¶
列表中元素的获取可以通过
a, b = [1, 2]operator.getitemsome_list[i]
| Python | |
|---|---|
2.2.3 Abstraction Barriers¶
当程序的一部分本可以使用更高层次的函数,却使用了较低层次的函数时,就会发生 barrier violation
抽象屏障使程序更易于维护和修改。
2.2.4 The Properties of Data¶
一般来说,我们可以使用selectors和constructors以及一些behavior来表达数据抽象。
| Python | |
|---|---|
Lab04 Tree Recursion, Data Abstraction¶
Lecture 14 Trees¶
Disc 05 Trees¶
Lecture 15 Mutability¶
Reading 2.4 Mutable Data¶
创建模块化程序的一种强大技术是引入可变数据。
In this way , a single data object can represent something that evolves independently of the rest of the program.
Adding state to data is a central ingredient
2.4.1 The Object Metaphor¶
Objects combine data values with behavior.
对象表示信息,同时也有与他们所表示的事物一样的行为。
对象的属性:<expression>.<name>
对象的方法:methods are functions that compute their results from both their arguments and their object.
数字、字符串、列表、range都是对象,都可以调用属性和方法。
| Python | |
|---|---|
Python中的所有值都是对象,都可以调用属性和方法。
2.4.2 Sequence Objects¶
可变对象用于表示随时间变化的值。
An object may have changing properties due to mutating operations.
Tuples. 元组是一种不可变对象。
2.4.3 Dictionaries¶
Dictionaries are Python's built-in data type for storing and manipulating correspondence relationships.
字典用于存储和操作对应关系。字典的键和值都是对象。字典的目的是提供一种抽象:由描述性键索引的值
- 字典的键不能是可变值,也不能包含可变值
- 对于给定的键,最多只能有一个值
2.4.4 Local State¶
Lists and dictionaries have local state: they are changing values that have some particular contents at any point in the execution of a program.
Only after a nonlocal statement can a function change the binding of names in these frames.
assignment statements already had a dual role: they either created new bindings or re-bound existing names
这种非局部赋值模式是具有高阶函数和词法作用域的编程语言的普遍特征。在解释器中,执行函数体之前预先计算有关函数体的事实是相当常见的。Python的预处理限制了局部变量可以出现的帧。
2.4.5 The Benefits of Non-local Assignment¶
非局部赋值是我们把程序视为独立自主对象的重要一步,这些对象彼此交互,但又各自管理自己的内部状态。
非局部赋值允许我们维护某种局部状态。
2.4.6 The Cost of Non-Local Assignment¶
我们需要思考两个名字是值相同,绑定的instance不同,还是本身就是相同的东西。
非局部赋值引入了复杂性,但仍是创建模块化程序的强大工具。利用具有局部状态的函数,我们能够实现可变数据类型。
2.4.7 Implementing Lists and Dictionaries¶
2.4.8 Dispatch Dictionaries¶
分发函数是实现抽象数据消息传递接口的通用方法。
2.4.9 Propagating Constraints 传播约束¶
Expressing programs as constraints is a type of declarative programming, in which a programmer declares the structure of a problem to be solved, but abstracts away the details of exactly how the solution to the problem is computed.
将程序表达为约束是一种声明式编程,在这种编程方式中,程序员声明待解决问题的结构,但抽象掉了问题解决方案具体如何计算的细节。
- 一旦某个连接器被赋值,它就唤醒所有与它相连、但不是刚刚给它赋值的那个约束;
- 每个被唤醒的约束检查自己关联的几个连接器,看是否已经有足够信息能推算出还缺值的那个连接器;
- 如果能推出来,就把值设给那个连接器,从而继续唤醒下一圈约束……
- 直到网络稳定、没有新信息可以传播为止。
- 谁赋的值就由谁来撤销:每个连接器会记住是谁(哪个约束或用户)给它赋的值(即 "informant"),只有这个来源才能让它"忘记"这个值;
- 矛盾检测:如果连接器已有值,又收到一个不同的值,就报告矛盾,而不是简单覆盖。
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
核心思想:把数据和行为捆绑在一起、通过"消息"来驱动交互、通过校验来源实现受控的状态改变。
