Data Abstraction Announcements Data Abstraction Data Abstraction - - PowerPoint PPT Presentation

data abstraction announcements data abstraction data
SMART_READER_LITE
LIVE PREVIEW

Data Abstraction Announcements Data Abstraction Data Abstraction - - PowerPoint PPT Presentation

Data Abstraction Announcements Data Abstraction Data Abstraction 4 Data Abstraction Compound values combine other values together 4 Data Abstraction Compound values combine other values together A date: a year, a month, and


slide-1
SLIDE 1

Data Abstraction

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Data Abstraction

slide-4
SLIDE 4

Data Abstraction

4

slide-5
SLIDE 5

Data Abstraction

  • Compound values combine other values together

4

slide-6
SLIDE 6

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day

4

slide-7
SLIDE 7

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

4

slide-8
SLIDE 8

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values as units

4

slide-9
SLIDE 9

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values as units
  • Isolate two parts of any program that uses data:

4

slide-10
SLIDE 10

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values as units
  • Isolate two parts of any program that uses data:

How data are represented (as parts)

4

slide-11
SLIDE 11

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values as units
  • Isolate two parts of any program that uses data:

How data are represented (as parts) How data are manipulated (as units)

4

slide-12
SLIDE 12

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values 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

4

slide-13
SLIDE 13

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values 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 All Programmers

4

slide-14
SLIDE 14

Data Abstraction

  • Compound values combine other values together

A date: a year, a month, and a day A geographic position: latitude and longitude

  • Data abstraction lets us manipulate compound values 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 All Programmers Great Programmers

4

slide-15
SLIDE 15

Rational Numbers

5

slide-16
SLIDE 16

Rational Numbers

numerator denominator

5

slide-17
SLIDE 17

Rational Numbers

Exact representation of fractions numerator denominator

5

slide-18
SLIDE 18

Rational Numbers

Exact representation of fractions A pair of integers numerator denominator

5

slide-19
SLIDE 19

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) numerator denominator

5

slide-20
SLIDE 20

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) Assume we can compose and decompose rational numbers: numerator denominator

5

slide-21
SLIDE 21

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) Assume we can compose and decompose rational numbers: numerator denominator

  • rational(n, d) returns a rational number x

5

slide-22
SLIDE 22

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) Assume we can compose and decompose rational numbers: numerator denominator

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x

5

slide-23
SLIDE 23

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) Assume we can compose and decompose rational numbers: numerator denominator

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

5

slide-24
SLIDE 24

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) Assume we can compose and decompose rational numbers: numerator denominator

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor

5

slide-25
SLIDE 25

Rational Numbers

Exact representation of fractions A pair of integers As soon as division occurs, the exact representation may be lost! (Demo) Assume we can compose and decompose rational numbers: numerator denominator

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor Selectors

5

slide-26
SLIDE 26

Rational Number Arithmetic

6

General Form Example

slide-27
SLIDE 27

Rational Number Arithmetic

3 2 3 5 *

6

General Form Example

slide-28
SLIDE 28

Rational Number Arithmetic

3 2 3 5 * 9 10 =

6

General Form Example

slide-29
SLIDE 29

Rational Number Arithmetic

3 2 3 5 * 9 10 = nx dx ny dy *

6

General Form Example

slide-30
SLIDE 30

Rational Number Arithmetic

3 2 3 5 * 9 10 = nx dx ny dy * nx*ny dx*dy =

6

General Form Example

slide-31
SLIDE 31

Rational Number Arithmetic

3 2 3 5 * 9 10 = 3 2 3 5 + nx dx ny dy * nx*ny dx*dy =

6

General Form Example

slide-32
SLIDE 32

Rational Number Arithmetic

3 2 3 5 * 9 10 = 3 2 3 5 + 21 10 = nx dx ny dy * nx*ny dx*dy =

6

General Form Example

slide-33
SLIDE 33

Rational Number Arithmetic

3 2 3 5 * 9 10 = 3 2 3 5 + 21 10 = nx dx ny dy * nx*ny dx*dy = nx dx ny dy +

6

General Form Example

slide-34
SLIDE 34

Rational Number Arithmetic

3 2 3 5 * 9 10 = 3 2 3 5 + 21 10 = nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

6

General Form Example

slide-35
SLIDE 35

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

7

nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-36
SLIDE 36

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

7

nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-37
SLIDE 37

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor

7

nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-38
SLIDE 38

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor

7

Selectors Selectors nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-39
SLIDE 39

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor

7

Selectors Selectors These functions implement an abstract representation for rational numbers nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-40
SLIDE 40

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy)

7

Selectors Selectors These functions implement an abstract representation for rational numbers nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-41
SLIDE 41

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy) def print_rational(x): print(numer(x), '/', denom(x))

7

Selectors Selectors These functions implement an abstract representation for rational numbers nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-42
SLIDE 42

def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))

Rational Number Arithmetic Implementation

  • rational(n, d) returns a rational number x
  • numer(x) returns the numerator of x
  • denom(x) returns the denominator of x

Constructor def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy) def print_rational(x): print(numer(x), '/', denom(x)) def rationals_are_equal(x, y): return numer(x) * denom(y) == numer(y) * denom(x)

7

Selectors Selectors These functions implement an abstract representation for rational numbers nx dx ny dy * nx*ny dx*dy = nx dx ny dy + nx*dy + ny*dx dx*dy =

slide-43
SLIDE 43

Pairs

slide-44
SLIDE 44

Representing Pairs Using Lists

9

slide-45
SLIDE 45

Representing Pairs Using Lists

9

>>> pair = [1, 2]

slide-46
SLIDE 46

Representing Pairs Using Lists

9

>>> pair = [1, 2] >>> pair [1, 2]

slide-47
SLIDE 47

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets

9

>>> pair = [1, 2] >>> pair [1, 2]

slide-48
SLIDE 48

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair

slide-49
SLIDE 49

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1

slide-50
SLIDE 50

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2

slide-51
SLIDE 51

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2

slide-52
SLIDE 52

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1

slide-53
SLIDE 53

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2

slide-54
SLIDE 54

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list Element selection using the selection operator

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2

slide-55
SLIDE 55

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list Element selection using the selection operator

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2 >>> from operator import getitem

slide-56
SLIDE 56

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list Element selection using the selection operator

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1

slide-57
SLIDE 57

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list Element selection using the selection operator

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2

slide-58
SLIDE 58

Representing Pairs Using Lists

A list literal: Comma-separated expressions in brackets "Unpacking" a list Element selection using the selection operator

9

>>> pair = [1, 2] >>> pair [1, 2] >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 Element selection function

slide-59
SLIDE 59

def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d]

Representing Rational Numbers

10

slide-60
SLIDE 60

def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d]

Representing Rational Numbers

Construct a list

10

slide-61
SLIDE 61

def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d]

Representing Rational Numbers

Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0]

10

slide-62
SLIDE 62

def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d]

Representing Rational Numbers

Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1]

10

slide-63
SLIDE 63

def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d]

Representing Rational Numbers

Construct a list Select item from a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1]

10

slide-64
SLIDE 64

def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d]

Representing Rational Numbers

Construct a list Select item from a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1]

10

(Demo)

slide-65
SLIDE 65

Reducing to Lowest Terms

Example:

11

slide-66
SLIDE 66

Reducing to Lowest Terms

Example: 3 2 5 3 *

11

slide-67
SLIDE 67

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 =

11

slide-68
SLIDE 68

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-69
SLIDE 69

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 15 6 1/3 1/3 * 5 2 =

11

slide-70
SLIDE 70

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-71
SLIDE 71

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-72
SLIDE 72

from fractions import gcd

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-73
SLIDE 73

from fractions import gcd def rational(n, d):

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-74
SLIDE 74

from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms."""

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-75
SLIDE 75

from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d)

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-76
SLIDE 76

from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g]

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 =

11

slide-77
SLIDE 77

from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g]

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 = Greatest common divisor

11

slide-78
SLIDE 78

from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g]

Reducing to Lowest Terms

Example: 3 2 5 3 * 5 2 = 2 5 1 10 + 1 2 = 25 50 1/25 1/25 * 1 2 = 15 6 1/3 1/3 * 5 2 = Greatest common divisor

11

(Demo)

slide-79
SLIDE 79

Abstraction Barriers

slide-80
SLIDE 80

Abstraction Barriers

13

slide-81
SLIDE 81

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using...

slide-82
SLIDE 82

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation

slide-83
SLIDE 83

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values

slide-84
SLIDE 84

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational

slide-85
SLIDE 85

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations

slide-86
SLIDE 86

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators

slide-87
SLIDE 87

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom

slide-88
SLIDE 88

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom

slide-89
SLIDE 89

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom Implement selectors and constructor for rationals

slide-90
SLIDE 90

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom Implement selectors and constructor for rationals two-element lists

slide-91
SLIDE 91

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom Implement selectors and constructor for rationals two-element lists list literals and element selection

slide-92
SLIDE 92

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom Implement selectors and constructor for rationals two-element lists list literals and element selection

slide-93
SLIDE 93

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom Implement selectors and constructor for rationals two-element lists list literals and element selection

Implementation of lists

slide-94
SLIDE 94

Abstraction Barriers

13

Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation whole data values add_rational, mul_rational rationals_are_equal, print_rational Create rationals or implement rational operations numerators and denominators rational, numer, denom Implement selectors and constructor for rationals two-element lists list literals and element selection

Implementation of lists

slide-95
SLIDE 95

Violating Abstraction Barriers

add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ]

14

slide-96
SLIDE 96

Does not use constructors

Violating Abstraction Barriers

add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ]

14

slide-97
SLIDE 97

Does not use constructors Twice!

Violating Abstraction Barriers

add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ]

14

slide-98
SLIDE 98

Does not use constructors Twice! No selectors!

Violating Abstraction Barriers

add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ]

14

slide-99
SLIDE 99

Does not use constructors Twice! No selectors! And no constructor!

Violating Abstraction Barriers

add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ]

14

slide-100
SLIDE 100

Violating Abstraction Barriers

14

slide-101
SLIDE 101

Data Representations

slide-102
SLIDE 102

What are Data?

16

slide-103
SLIDE 103

What are Data?

  • We need to guarantee that constructor and selector functions work

together to specify the right behavior

16

slide-104
SLIDE 104

What are 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 n/d

16

slide-105
SLIDE 105

What are 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 n/d

  • Data abstraction uses selectors and constructors to define behavior

16

slide-106
SLIDE 106

What are 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 n/d

  • Data abstraction uses selectors and constructors to define behavior
  • If behavior conditions are met, then the representation is valid

16

slide-107
SLIDE 107

What are 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 n/d

  • Data abstraction uses selectors and constructors to define behavior
  • If behavior conditions are met, then the representation is valid

You can recognize an abstract data representation by its behavior

16

slide-108
SLIDE 108

What are 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 n/d

  • Data abstraction uses selectors and constructors to define behavior
  • If behavior conditions are met, then the representation is valid

You can recognize an abstract data representation by its behavior

16

(Demo)

slide-109
SLIDE 109

Rationals Implemented as Functions

17

slide-110
SLIDE 110

def rational(n, d): def select(name): if name == 'n': return n elif name == 'd': return d return select def numer(x): return x('n') def denom(x): return x('d')

Rationals Implemented as Functions

17

slide-111
SLIDE 111

def rational(n, d): def select(name): if name == 'n': return n elif name == 'd': return d return select def numer(x): return x('n') def denom(x): return x('d')

This function represents a rational number

Rationals Implemented as Functions

17

slide-112
SLIDE 112

def rational(n, d): def select(name): if name == 'n': return n elif name == 'd': return d return select def numer(x): return x('n') def denom(x): return x('d')

This function represents a rational number

Rationals Implemented as Functions

Constructor is a higher-order function

17

slide-113
SLIDE 113

def rational(n, d): def select(name): if name == 'n': return n elif name == 'd': return d return select def numer(x): return x('n') def denom(x): return x('d')

This function represents a rational number

Rationals Implemented as Functions

Constructor is a higher-order function Selector calls x

17

slide-114
SLIDE 114

def rational(n, d): def select(name): if name == 'n': return n elif name == 'd': return d return select def numer(x): return x('n') def denom(x): return x('d')

This function represents a rational number

Rationals Implemented as Functions

Constructor is a higher-order function Selector calls x

17

x = rational(3, 8) numer(x)

slide-115
SLIDE 115

def rational(n, d): def select(name): if name == 'n': return n elif name == 'd': return d return select def numer(x): return x('n') def denom(x): return x('d')

This function represents a rational number

Rationals Implemented as Functions

Constructor is a higher-order function Selector calls x

17

pythontutor.com/composingprograms.html#code=def%20rational%28n, %20d%29%3A%0A%20%20%20%20def%20select%28name%29%3A%0A%20%20%20%20%20%20%20%20if%20name%20%3D%3D%20'n'%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20n%0A%20%20%20%20%20%20%20%20elif%20name%20%3D%3D%20'd'%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20d%0A%20%20%20%20return%20select% 0A%20%20%20%20%0Adef%20numer%28x%29%3A%0A%20%20%20%20return%20x%28'n'%29%0A%0Adef%20denom%28x%29%3A%0A%20%20%20%20return%20x%28'd'%29%0A%20%20%20%20%0Ax%20%3D%20rational%283,%208%29%0Anumer%28x%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

x = rational(3, 8) numer(x)

slide-116
SLIDE 116

Dictionaries

{'Dem': 0}

slide-117
SLIDE 117

Limitations on Dictionaries

19

slide-118
SLIDE 118

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs

19

slide-119
SLIDE 119

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

19

slide-120
SLIDE 120

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)

19

slide-121
SLIDE 121

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

19

slide-122
SLIDE 122

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

This first restriction is tied to Python's underlying implementation of dictionaries

19

slide-123
SLIDE 123

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

This first restriction is tied to Python's underlying implementation of dictionaries The second restriction is part of the dictionary abstraction

19

slide-124
SLIDE 124

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

This first restriction is tied to Python's underlying implementation of dictionaries The second restriction is part of the dictionary abstraction If you want to associate multiple values with a key, store them all in a sequence value

19