61A Lecture 18 Friday, March 6 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 18
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 18 Friday, March 6 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 18 Friday, March 6 Announcements 2 Announcements Project 3 due Thursday 3/12 @ 11:59pm (get started now!) 2 Announcements Project 3 due Thursday 3/12 @ 11:59pm (get started now!) Project party on Tuesday 3/10


slide-1
SLIDE 1

61A Lecture 18

Friday, March 6

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

2

slide-4
SLIDE 4

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

§Project party on Tuesday 3/10 5pm-6:30pm in 2050 VLSB

2

slide-5
SLIDE 5

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

§Project party on Tuesday 3/10 5pm-6:30pm in 2050 VLSB §Bonus point for early submission by Wednesday 3/11

2

slide-6
SLIDE 6

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

§Project party on Tuesday 3/10 5pm-6:30pm in 2050 VLSB §Bonus point for early submission by Wednesday 3/11

  • Homework 6 due Monday 3/16 @ 11:59pm (not yet released)

2

slide-7
SLIDE 7

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

§Project party on Tuesday 3/10 5pm-6:30pm in 2050 VLSB §Bonus point for early submission by Wednesday 3/11

  • Homework 6 due Monday 3/16 @ 11:59pm (not yet released)
  • Midterm 2 is on Thursday 3/19 7pm-9pm

2

slide-8
SLIDE 8

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

§Project party on Tuesday 3/10 5pm-6:30pm in 2050 VLSB §Bonus point for early submission by Wednesday 3/11

  • Homework 6 due Monday 3/16 @ 11:59pm (not yet released)
  • Midterm 2 is on Thursday 3/19 7pm-9pm

§Emphasis: mutable data, object-oriented programming, recursion, and recursive data

2

slide-9
SLIDE 9

Announcements

  • Project 3 due Thursday 3/12 @ 11:59pm (get started now!)

§Project party on Tuesday 3/10 5pm-6:30pm in 2050 VLSB §Bonus point for early submission by Wednesday 3/11

  • Homework 6 due Monday 3/16 @ 11:59pm (not yet released)
  • Midterm 2 is on Thursday 3/19 7pm-9pm

§Emphasis: mutable data, object-oriented programming, recursion, and recursive data §Fill out conflict form if you cannot attend due to a course conflict

2

slide-10
SLIDE 10

Hog Contest Results

3

slide-11
SLIDE 11

Hog Contest Results

Excellent participation! 51 qualified submissions Lots of excellent ideas

3

slide-12
SLIDE 12

Hog Contest Results

Excellent participation! 51 qualified submissions Lots of excellent ideas

3

slide-13
SLIDE 13

Hog Contest Results

Excellent participation! 51 qualified submissions Lots of excellent ideas

3

(Results)

slide-14
SLIDE 14

Type Coercion

slide-15
SLIDE 15

Review: Type Dispatching Analysis

5

Minimal violation of abstraction barriers: we define cross-type functions as necessary. Extensible: Any new numeric type can "install" itself into the existing system by adding new entries to the cross-type function dictionaries

slide-16
SLIDE 16

Review: Type Dispatching Analysis

Arg 1 Arg 2 Add Multiply Complex Complex Rational Rational Complex Rational Rational Complex

5

Minimal violation of abstraction barriers: we define cross-type functions as necessary. Extensible: Any new numeric type can "install" itself into the existing system by adding new entries to the cross-type function dictionaries

slide-17
SLIDE 17

Coercion

6

slide-18
SLIDE 18

Coercion

Idea: Some types can be converted into other types

6

slide-19
SLIDE 19

Coercion

Idea: Some types can be converted into other types Takes advantage of structure in the type system

6

slide-20
SLIDE 20

Coercion

Idea: Some types can be converted into other types Takes advantage of structure in the type system def rational_to_complex(r): """Return complex equal to rational.""" return ComplexRI(r.numer/r.denom, 0)

6

slide-21
SLIDE 21

Coercion

Idea: Some types can be converted into other types Takes advantage of structure in the type system def rational_to_complex(r): """Return complex equal to rational.""" return ComplexRI(r.numer/r.denom, 0) Question: Can any numeric type be coerced into any other?

6

slide-22
SLIDE 22

Coercion

Idea: Some types can be converted into other types Takes advantage of structure in the type system def rational_to_complex(r): """Return complex equal to rational.""" return ComplexRI(r.numer/r.denom, 0) Question: Can any numeric type be coerced into any other?

6

Question: Can any two numeric types be coerced into a common type?

slide-23
SLIDE 23

Coercion

Idea: Some types can be converted into other types Takes advantage of structure in the type system def rational_to_complex(r): """Return complex equal to rational.""" return ComplexRI(r.numer/r.denom, 0) Question: Can any numeric type be coerced into any other?

6

Question: Can any two numeric types be coerced into a common type? Question: Is coercion exact?

slide-24
SLIDE 24

Applying Operators with Coercion

7

slide-25
SLIDE 25

Applying Operators with Coercion

7

class Number:

slide-26
SLIDE 26

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y)

slide-27
SLIDE 27

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) Always defer to add method

slide-28
SLIDE 28

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): Always defer to add method

slide-29
SLIDE 29

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other Always defer to add method

slide-30
SLIDE 30

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other Always defer to add method Same interface: 
 no change required

slide-31
SLIDE 31

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other elif (self.type_tag, other.type_tag) in self.coercions: Always defer to add method Same interface: 
 no change required

slide-32
SLIDE 32

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other elif (self.type_tag, other.type_tag) in self.coercions: coercions = {('rat', 'com'): rational_to_complex} Always defer to add method Same interface: 
 no change required

slide-33
SLIDE 33

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other elif (self.type_tag, other.type_tag) in self.coercions: return (self.coerce_to(other.type_tag), other) coercions = {('rat', 'com'): rational_to_complex} Always defer to add method Same interface: 
 no change required

slide-34
SLIDE 34

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other elif (self.type_tag, other.type_tag) in self.coercions: return (self.coerce_to(other.type_tag), other) coercions = {('rat', 'com'): rational_to_complex} Always defer to add method Same interface: 
 no change required def coerce_to(self, other_tag): coercion_fn = self.coercions[(self.type_tag, other_tag)] return coercion_fn(self)

slide-35
SLIDE 35

Applying Operators with Coercion

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other elif (self.type_tag, other.type_tag) in self.coercions: return (self.coerce_to(other.type_tag), other) elif (other.type_tag, self.type_tag) in self.coercions: return (self, other.coerce_to(self.type_tag)) coercions = {('rat', 'com'): rational_to_complex} Always defer to add method Same interface: 
 no change required def coerce_to(self, other_tag): coercion_fn = self.coercions[(self.type_tag, other_tag)] return coercion_fn(self)

slide-36
SLIDE 36

Applying Operators with Coercion

(Demo)

7

class Number: def __add__(self, other): x, y = self.coerce(other) return x.add(y) def coerce(self, other): if self.type_tag == other.type_tag: return self, other elif (self.type_tag, other.type_tag) in self.coercions: return (self.coerce_to(other.type_tag), other) elif (other.type_tag, self.type_tag) in self.coercions: return (self, other.coerce_to(self.type_tag)) coercions = {('rat', 'com'): rational_to_complex} Always defer to add method Same interface: 
 no change required def coerce_to(self, other_tag): coercion_fn = self.coercions[(self.type_tag, other_tag)] return coercion_fn(self)

slide-37
SLIDE 37

Coercion Analysis

8

slide-38
SLIDE 38

Coercion Analysis

Minimal violation of abstraction barriers: we define cross-type coercion as necessary

8

slide-39
SLIDE 39

Coercion Analysis

Minimal violation of abstraction barriers: we define cross-type coercion as necessary Requires that all types can be coerced into a common type

8

slide-40
SLIDE 40

Coercion Analysis

Minimal violation of abstraction barriers: we define cross-type coercion as necessary Requires that all types can be coerced into a common type More sharing: All operators use the same coercion scheme

8

slide-41
SLIDE 41

Coercion Analysis

Minimal violation of abstraction barriers: we define cross-type coercion as necessary Requires that all types can be coerced into a common type More sharing: All operators use the same coercion scheme Arg 1 Arg 2 Add Multiply Complex Complex Rational Rational Complex Rational Rational Complex

8

slide-42
SLIDE 42

Coercion Analysis

Minimal violation of abstraction barriers: we define cross-type coercion as necessary Requires that all types can be coerced into a common type More sharing: All operators use the same coercion scheme Arg 1 Arg 2 Add Multiply Complex Complex Rational Rational Complex Rational Rational Complex From To Coerce Complex Rational Rational Complex

8

slide-43
SLIDE 43

Coercion Analysis

Minimal violation of abstraction barriers: we define cross-type coercion as necessary Requires that all types can be coerced into a common type More sharing: All operators use the same coercion scheme Arg 1 Arg 2 Add Multiply Complex Complex Rational Rational Complex Rational Rational Complex From To Coerce Complex Rational Rational Complex Type Add Multiply Complex Rational

8

slide-44
SLIDE 44

Linked Lists

slide-45
SLIDE 45

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

slide-46
SLIDE 46

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5

slide-47
SLIDE 47

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 first: 3 rest: Link instance

slide-48
SLIDE 48

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance

slide-49
SLIDE 49

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance

slide-50
SLIDE 50

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance

slide-51
SLIDE 51

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair

slide-52
SLIDE 52

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value

slide-53
SLIDE 53

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list

slide-54
SLIDE 54

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list A class attribute represents an empty linked list

slide-55
SLIDE 55

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

10

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link(3, Link(4, Link(5, Link.empty))) A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list A class attribute represents an empty linked list

slide-56
SLIDE 56

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

11

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-57
SLIDE 57

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

11

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-58
SLIDE 58

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

11

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-59
SLIDE 59

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

11

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-60
SLIDE 60

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

11

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance , Link.empty ) Link(3, Link(4, Link(5 )))

slide-61
SLIDE 61

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

11

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link(3, Link(4, Link(5 )))

slide-62
SLIDE 62

Linked List Class

12

Link(3, Link(4, Link(5 )))

slide-63
SLIDE 63

Linked List Class

12

Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))

slide-64
SLIDE 64

Linked List Class

class Link:

12

Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))

slide-65
SLIDE 65

Linked List Class

class Link:

12

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): Link(3, Link(4, Link(5 )))

slide-66
SLIDE 66

Linked List Class

class Link:

12

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) Link(3, Link(4, Link(5 )))

slide-67
SLIDE 67

Linked List Class

class Link:

12

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 )))

slide-68
SLIDE 68

Linked List Class

class Link:

12

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link

slide-69
SLIDE 69

Linked List Class

class Link:

12

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-70
SLIDE 70

Linked List Class

class Link: empty = ()

12

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-71
SLIDE 71

Linked List Class

class Link: empty = ()

12

Some zero-length sequence Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-72
SLIDE 72

Linked List Class

class Link: empty = ()

12

Some zero-length sequence Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest (Demo) Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-73
SLIDE 73

Sequence Operations

slide-74
SLIDE 74

Linked List Class

14

More special method names: __len__ __getitem__ Element selection [] Built-in len function

slide-75
SLIDE 75

Linked List Class

14

More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences

slide-76
SLIDE 76

Linked List Class

class Link: empty = ()

14

More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest

slide-77
SLIDE 77

Linked List Class

class Link: empty = ()

14

More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1]

slide-78
SLIDE 78

Linked List Class

class Link: empty = ()

14

This element selection syntax More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1]

slide-79
SLIDE 79

Linked List Class

class Link: empty = ()

14

This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1]

slide-80
SLIDE 80

Linked List Class

class Link: empty = ()

14

This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1] def __len__(self): return 1 + len(self.rest)

slide-81
SLIDE 81

Linked List Class

class Link: empty = () Recursive call to __len__

14

This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1] def __len__(self): return 1 + len(self.rest)

slide-82
SLIDE 82

Linked List Class

class Link: empty = () Recursive call to __len__

14

(Demo) Methods can be recursive too! This element selection syntax Calls this method More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences def __init__(self, first, rest=empty): assert ... self.first = first self.rest = rest def __getitem__(self, i): if i == 0: return self.first else: return self.rest[i-1] def __len__(self): return 1 + len(self.rest)

slide-83
SLIDE 83

Linked List Processing

(Demo)