SLIDE 1
61A Lecture 18 Announcements Sequences The Sequence Abstraction 4 - - PowerPoint PPT Presentation
61A Lecture 18 Announcements Sequences The Sequence Abstraction 4 - - PowerPoint PPT Presentation
61A Lecture 18 Announcements Sequences The Sequence Abstraction 4 The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 4 The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one
SLIDE 2
SLIDE 3
Sequences
SLIDE 4
The Sequence Abstraction
4
SLIDE 5
The Sequence Abstraction
red, orange, yellow, green, blue, indigo, violet.
4
SLIDE 6
The Sequence Abstraction
There isn't just one sequence class or data abstraction (in Python or in general). red, orange, yellow, green, blue, indigo, violet.
4
SLIDE 7
The Sequence Abstraction
There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.
4
SLIDE 8
The Sequence Abstraction
There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.
- Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0.
4
SLIDE 9
The Sequence Abstraction
There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.
- Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 0 , 1 , 2 , 3 , 4 , 5 , 6 .
4
SLIDE 10
The Sequence Abstraction
There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.
- Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There is built-in syntax associated with this behavior, or we can use functions.
4
SLIDE 11
The Sequence Abstraction
There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.
- Length. A sequence has a finite length.
Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There is built-in syntax associated with this behavior, or we can use functions. A list is a kind of built-in sequence
4
SLIDE 12
Linked Lists
SLIDE 13
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
SLIDE 14
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
3 , 4 , 5
SLIDE 15
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
3 , 4 , 5 first: 3 rest: Link instance
SLIDE 16
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance
SLIDE 17
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance
SLIDE 18
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance
SLIDE 19
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
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 20
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
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 21
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
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 22
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
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 23
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
6
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 24
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
7
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 25
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
7
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 26
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
7
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 27
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
7
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 28
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
7
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 29
Linked List Structure
A linked list is either empty or a first value and the rest of the linked list
7
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 30
Linked List Class
8
Link(3, Link(4, Link(5 )))
SLIDE 31
Linked List Class
8
Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))
SLIDE 32
Linked List Class
class Link:
8
Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))
SLIDE 33
Linked List Class
class Link:
8
Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): Link(3, Link(4, Link(5 )))
SLIDE 34
Linked List Class
class Link:
8
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 35
Linked List Class
class Link:
8
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 36
Linked List Class
class Link:
8
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 37
Linked List Class
class Link:
8
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 38
Linked List Class
class Link: empty = ()
8
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 39
Linked List Class
class Link: empty = ()
8
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 40
Linked List Class
class Link: empty = ()
8
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 41
Sequence Operations
SLIDE 42
Linked List Class
10
More special method names: __len__ __getitem__ Element selection [] Built-in len function
SLIDE 43
Linked List Class
10
More special method names: __len__ __getitem__ Element selection [] Built-in len function Linked lists are sequences
SLIDE 44
Linked List Class
class Link: empty = ()
10
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 45
Linked List Class
class Link: empty = ()
10
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 46
Linked List Class
class Link: empty = ()
10
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 47
Linked List Class
class Link: empty = ()
10
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 48
Linked List Class
class Link: empty = ()
10
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 49
Linked List Class
class Link: empty = () Recursive call to __len__
10
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 50
Linked List Class
class Link: empty = () Recursive call to __len__
10
(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 51
Property Methods
SLIDE 52
Property Methods
Often, we want the value of instance attributes to stay in sync
12
For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 53
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5)))
12
For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 54
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4
12
For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 55
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6
12
For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 56
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6
12
For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 57
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5)))
12
For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 58
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5)))
12
No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 59
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance
12
No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 60
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance A @<attribute>.setter decorator on a method designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.
12
No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 61
Property Methods
Often, we want the value of instance attributes to stay in sync >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance (Demo) A @<attribute>.setter decorator on a method designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.
12
No method calls! For example, what if we wanted a Ratio to keep its proportion when its numerator changes
SLIDE 62