当前位置: 首页 > news >正文

伯克利 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 有效的


http://www.mrgr.cn/news/90639.html

相关文章:

  • 探索ELK 的魅力
  • FPGA简介|结构、组成和应用
  • [创业之路-299]:图解金融体系结构
  • java商城解决方案
  • Datawhale 数学建模导论二 2025年2月
  • 【C++八股】静态局部变量/全局变量/局部变量的区别和使用场景
  • mapbox 从入门到精通 - 目录
  • LeapMotion第2代 Unity示范代码(桌面开发)
  • Java IO流详解
  • 二次封装axios解决异步通信痛点
  • #渗透测试#批量漏洞挖掘#致远互联AnalyticsCloud 分析云 任意文件读取
  • 【一文读懂】HTTP与Websocket协议
  • 【07】trait特性
  • 综合与时序分析的设计约束(4)—— 异常
  • ARM64 Trust Firmware [一]
  • 编码格式大全解释以及相关编码特性
  • LTSPICE仿真电路:(二十三)单端信号转差分信号的简单仿真
  • MybatisPlus常用增删改查
  • springcloud集成gateway
  • (Windows | Linux)ssh访问服务器报错:no matching key exchange method found
  • #渗透测试#批量漏洞挖掘#Crocus系统—Download 文件读取
  • 用于处理元素的全屏显示和退出全屏操作--useFullScreen
  • React进阶之React核心源码解析(一)
  • 【CXX】0 Rust与C ++的互操作利器:CXX库介绍与示例
  • 【Linux】Ubuntu Linux 系统——Python集成开发环境
  • C语言中printf()函数,格式输出符