伯克利 CS61A 课堂笔记 09 —— Data Abstraction
本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。
目录
01 Data Abstraction 数据抽象
Ⅰ Rational Numbers
Ⅱ Rational Number Arithmetic
02 Pairs 对
Ⅰ Representing Pairs Using Lists
Ⅱ Reducing to Lowest Terms
03 Abstraction Barriers 抽象障碍
04 Data Representations 数据表示
Ⅰ What is Data?
Ⅱ Demo
Ⅲ Rational Data Abstraction Implemented as Functions
01 Data Abstraction 数据抽象
Compound objects combine objects together:
A date: a year, a month, and a day.
A geographic position: latitude and longitude.
An abstract data type lets us manipulate compound objects as units.
Isolate two parts of any program that uses data:
How data are represented (as parts).
How data are manipulated (as units).
Data abstraction: A methodology by which functions enforce an abstraction barrier between representation and use.
Ⅰ Rational Numbers
Exact representation of fractions is a pair of integers. However, as soon as division occurs, the exact representation may be lost !
Assume we can compose and decompose rational numbers:
Ⅱ Rational Number Arithmetic
#最上层def mul_rational(x, y):"""Multiply rational numbers x and y."""return rational(numer(x) * numer(y),denom(x) * denom(y))def add_rational(x, y):"""Add rational numbers x and y."""return rational(numer(x) * denom(y) + numer(y) * denom(x),denom(x) * denom(y))def equal_rational(x, y):"""Return whether rational numbers x and y are equal."""return numer(x) * denom(y) == numer(y) * denom(x)
rational(n, d) returns a rational number x.
numer(x) returns the numerator of x.
denom(x) returns the denominator of x.
These functions implement an abstract data type for rational numbers.
02 Pairs 对
Ⅰ Representing Pairs Using Lists
#最底层#A list literal: Comma-seperated expressions in brackets
>>> pair = [1, 2]
>>> pair
[1, 2]#"Unpacking" a list
>>> a, b = pair
>>> a
1
>>> b
2#Element selection using the selection operator
>>> pair[0]
1
>>> pair[1]
2#Element selection function
>>> from operator import getitem
>>> getitem(pair, 0)
1
>>> getitem(pair, 1)
2
#中间层def rational(n, d):"""Construct a rational number that represents N/D."""return [n, d] #Construct a listdef numer(x):"""Return the numerator of rational number X."""return x[0] #Select item from a listdef denom(x):"""Return the denominator of rational number X."""return x[1]
Ⅱ Reducing to Lowest Terms
from fractions import gcd #Greatest common divisor
def rational(n, d):"""Construct a rational number that represents N/D."""g = gcd(n, d)return (n // g, d // g)
03 Abstraction Barriers 抽象障碍
Violating abstraction barriers:
04 Data Representations 数据表示
Ⅰ What is Data?
We need to guarantee that constructor and selector functions work together to specify the right behavior.
Behavior condition: If we construct rational number x from numerator n and denominator d, then numer(x)/denom(x) must equal to n/d.
Data abstraction uses selectors and constructors to define behavior.
If behavior conditions are met, then the representation is valid.
Ⅱ Demo
#Rational arithmeticdef mul_rational(x, y):return rational(numer(x) * numer(y),denom(x) * denom(y))def add_rational(x, y):return rational(numer(x) * denom(y) + numer(y) * denom(x),denom(x) * denom(y))def equal_rational(x, y):return numer(x) * denom(y) == numer(y) * denom(x)def print_rational(x):print(numer(x), "/", denom(x))# Constructor and selectorsdef rational(n, d):return [n, d] def numer(x):return x[0] def denom(x):return x[1]
>>> x, y = rational(1, 2), rational(3, 8)
>>> print_rational(mul_rational(x, y))
3 / 16
# Constructor and selectorsdef rational(n, d):def select(name):if name == 'n':return nelif name == 'd':return dreturn selectdef numer(x):return x('n') def denom(x):return x('d')
>>> x, y = rational(1, 2), rational(3, 8)
>>> print_rational(mul_rational(x, y))
3 / 16
>>> x
<function rational.<locals>.select at 0x10293e6a8>
Ⅲ Rational Data Abstraction Implemented as Functions
附:词汇解释
latitude / ˈlætɪtuːd / 纬度、longitude / ˈlɑːndʒɪtuːd / 经度、represent 表示、manipulate / məˈnɪpjuleɪt / 操作、numerator / ˈnuːməreɪtər / 分子、denominator / dɪˈnɑːmɪneɪtər / 分母、fraction / ˈfrækʃ(ə)n / 分数、exact representation 精确表示、rational numbers 有理数、compose / kəmˈpoʊz / 构成、decompose / ˌdiːkəmˈpoʊz / 分解、arithmetic / əˈrɪθmətɪk / 算术,演算、constructor 构造器、selector 选择器、selection operator 选择操作符、bracket / ˈbrækɪt / 中括号、parentheses / pəˈrenθəsiːz / 小括号、lowest terms 最简形式、violate / ˈvaɪəleɪt / 违背、list literals 文字列表、element selection 元素选择、rational operation 有理运算、guarantee / ˌɡærənˈtiː / 确保、specify / ˈspesɪfaɪ / 明确指出,具体说明、met 符合、valid 有效的