61A Lecture 12 Announcements Objects (Demo) Objects 4 Objects - - PowerPoint PPT Presentation

61a lecture 12 announcements objects
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 12 Announcements Objects (Demo) Objects 4 Objects - - PowerPoint PPT Presentation

61A Lecture 12 Announcements Objects (Demo) Objects 4 Objects Objects represent information 4 Objects Objects represent information They consist of data and behavior, bundled together to create abstractions 4 Objects


slide-1
SLIDE 1

61A Lecture 12

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Objects

(Demo)

slide-4
SLIDE 4

Objects

4

slide-5
SLIDE 5

Objects

  • Objects represent information

4

slide-6
SLIDE 6

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions

4

slide-7
SLIDE 7

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes

4

slide-8
SLIDE 8

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python

4

slide-9
SLIDE 9

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:

4

slide-10
SLIDE 10

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs

4

slide-11
SLIDE 11

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs

4

slide-12
SLIDE 12

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object

4

slide-13
SLIDE 13

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object
  • All objects have attributes

4

slide-14
SLIDE 14

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object
  • All objects have attributes
  • A lot of data manipulation happens through object methods

4

slide-15
SLIDE 15

Objects

  • Objects represent information
  • They consist of data and behavior, bundled together to create abstractions
  • Objects can represent things, but also properties, interactions, & processes
  • A type of object is called a class; classes are first-class values in Python
  • Object-oriented programming:
  • A metaphor for organizing large programs
  • Special syntax that can improve the composition of programs
  • In Python, every value is an object
  • All objects have attributes
  • A lot of data manipulation happens through object methods
  • Functions do one thing; objects do many related things

4

slide-16
SLIDE 16

Example: Strings

(Demo)

slide-17
SLIDE 17

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange

6

slide-18
SLIDE 18

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits

6

slide-19
SLIDE 19

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

6

slide-20
SLIDE 20

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code

6

slide-21
SLIDE 21

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset

6

slide-22
SLIDE 22

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

6

slide-23
SLIDE 23

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n)

6

slide-24
SLIDE 24

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n) "Bell" (\a)

6

slide-25
SLIDE 25

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n) "Bell" (\a)

6

(Demo)

slide-26
SLIDE 26

Representing Strings: the Unicode Standard

7

slide-27
SLIDE 27

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

7

slide-28
SLIDE 28

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters

7

slide-29
SLIDE 29

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)

7

slide-30
SLIDE 30

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

7

slide-31
SLIDE 31

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order

7

slide-32
SLIDE 32

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

7

slide-33
SLIDE 33

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

7

slide-34
SLIDE 34

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE

7

slide-35
SLIDE 35

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

7

slide-36
SLIDE 36

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

'☺'

7

slide-37
SLIDE 37

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

'☺' '☹'

7

slide-38
SLIDE 38

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

'☺' '☹'

7

(Demo)

slide-39
SLIDE 39

Mutation Operations

slide-40
SLIDE 40

Some Objects Can Change

9

[Demo]

slide-41
SLIDE 41

Some Objects Can Change

First example in the course of an object changing state

9

[Demo]

slide-42
SLIDE 42

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

slide-43
SLIDE 43

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👷

same_person

slide-44
SLIDE 44

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👷

BABY same_person

slide-45
SLIDE 45

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👷

BABY same_person Unicode character name

slide-46
SLIDE 46

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👨

GIRL same_person Unicode character name

slide-47
SLIDE 47

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👨

GIRL jessica same_person Unicode character name

slide-48
SLIDE 48

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👪

WOMAN jessica same_person Unicode character name

slide-49
SLIDE 49

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER
 WOMAN jessica same_person Unicode character name

slide-50
SLIDE 50

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER
 WOMAN jessica same_person Unicode character name All names that refer to the same object are affected by a mutation

slide-51
SLIDE 51

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER
 WOMAN jessica same_person Unicode character name All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries

slide-52
SLIDE 52

Some Objects Can Change

First example in the course of an object changing state The same object can change in value throughout the course of computation

9

[Demo]

👶

OLDER
 WOMAN jessica same_person Unicode character name All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries {Demo}

slide-53
SLIDE 53

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

slide-54
SLIDE 54

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4]

slide-55
SLIDE 55

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4

slide-56
SLIDE 56

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four)

slide-57
SLIDE 57

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2

slide-58
SLIDE 58

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 def mystery(s): s.pop() s.pop() Interactive Diagram

slide-59
SLIDE 59

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 def mystery(s): s.pop() s.pop() Interactive Diagram def mystery(s): s[2:] = []

  • r
slide-60
SLIDE 60

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] def mystery(s): s.pop() s.pop() Interactive Diagram def mystery(s): s[2:] = []

  • r
slide-61
SLIDE 61

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 def mystery(s): s.pop() s.pop() Interactive Diagram def mystery(s): s[2:] = []

  • r
slide-62
SLIDE 62

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! def mystery(s): s.pop() s.pop() Interactive Diagram def mystery(s): s[2:] = []

  • r
slide-63
SLIDE 63

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! >>> len(four) 2 def mystery(s): s.pop() s.pop() Interactive Diagram def mystery(s): s[2:] = []

  • r
slide-64
SLIDE 64

Mutation Can Happen Within a Function Call

A function can change the value of any object in its scope

10

>>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! >>> len(four) 2 def mystery(s): s.pop() s.pop() def another_mystery(): four.pop() four.pop() Interactive Diagram def mystery(s): s[2:] = []

  • r
slide-65
SLIDE 65

Tuples

(Demo)

slide-66
SLIDE 66

Tuples are Immutable Sequences

12

slide-67
SLIDE 67

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

slide-68
SLIDE 68

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3)

slide-69
SLIDE 69

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze()

slide-70
SLIDE 70

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle

slide-71
SLIDE 71

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3)

slide-72
SLIDE 72

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3]

slide-73
SLIDE 73

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze()

slide-74
SLIDE 74

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle

slide-75
SLIDE 75

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!']

slide-76
SLIDE 76

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

>>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] Next lecture: ooze can change turtle's binding

slide-77
SLIDE 77

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] Next lecture: ooze can change turtle's binding

slide-78
SLIDE 78

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] Name change: Next lecture: ooze can change turtle's binding

slide-79
SLIDE 79

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Next lecture: ooze can change turtle's binding

slide-80
SLIDE 80

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 Next lecture: ooze can change turtle's binding

slide-81
SLIDE 81

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 4 Next lecture: ooze can change turtle's binding

slide-82
SLIDE 82

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 4 >>> x = 3 Next lecture: ooze can change turtle's binding

slide-83
SLIDE 83

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: >>> x = 2 4 >>> x = 3 6 Next lecture: ooze can change turtle's binding

slide-84
SLIDE 84

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 Next lecture: ooze can change turtle's binding

slide-85
SLIDE 85

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-86
SLIDE 86

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-87
SLIDE 87

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-88
SLIDE 88

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-89
SLIDE 89

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-90
SLIDE 90

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-91
SLIDE 91

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-92
SLIDE 92

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-93
SLIDE 93

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-94
SLIDE 94

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-95
SLIDE 95

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-96
SLIDE 96

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> s >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-97
SLIDE 97

Tuples are Immutable Sequences

Immutable values are protected from mutation

12

The value of an expression can change because of changes in names or objects An immutable sequence may still change if it contains a mutable value as an element >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle ['Anything could be inside!'] >>> x + x >>> x + x Name change: Object mutation: >>> x = 2 4 >>> x = 3 6 >>> x = [1, 2] [1, 2, 1, 2] >>> x.append(3) [1, 2, 3, 1, 2, 3] >>> s = ([1, 2], 3) >>> s[0] = 4 ERROR >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> s ([4, 2], 3) >>> x + x >>> x + x Next lecture: ooze can change turtle's binding

slide-98
SLIDE 98

Mutation

slide-99
SLIDE 99

Sameness and Change

14

slide-100
SLIDE 100

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces

14

slide-101
SLIDE 101

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator

14

slide-102
SLIDE 102

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change

14

slide-103
SLIDE 103

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed

14

slide-104
SLIDE 104

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

slide-105
SLIDE 105

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10]

slide-106
SLIDE 106

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a

slide-107
SLIDE 107

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True

slide-108
SLIDE 108

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20)

slide-109
SLIDE 109

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True

slide-110
SLIDE 110

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20]

slide-111
SLIDE 111

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-112
SLIDE 112

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-113
SLIDE 113

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-114
SLIDE 114

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-115
SLIDE 115

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-116
SLIDE 116

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-117
SLIDE 117

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a [10] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-118
SLIDE 118

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a [10] >>> b [10, 20] >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-119
SLIDE 119

Sameness and Change

  • As long as we never modify objects, a compound object is just the totality of its pieces
  • A rational number is just its numerator and denominator
  • This view is no longer valid in the presence of change
  • A compound data object has an "identity" in addition to the pieces of which it is composed
  • A list is still "the same" list even if we change its contents
  • Conversely, we could have two lists that happen to have the same contents, but are different

14

>>> a = [10] >>> b = [10] >>> a == b True >>> b.append(20) >>> a [10] >>> b [10, 20] >>> a == b False >>> a = [10] >>> b = a >>> a == b True >>> a.append(20) >>> a == b True >>> a [10, 20] >>> b [10, 20]

slide-120
SLIDE 120

Identity Operators

15

slide-121
SLIDE 121

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object

15

slide-122
SLIDE 122

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values

15

slide-123
SLIDE 123

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values Identical objects are always equal values

15

slide-124
SLIDE 124

Identity Operators

Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values Identical objects are always equal values

15

(Demo)

slide-125
SLIDE 125

Mutable Default Arguments are Dangerous

16

slide-126
SLIDE 126

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

slide-127
SLIDE 127

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ...

slide-128
SLIDE 128

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1

slide-129
SLIDE 129

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2

slide-130
SLIDE 130

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

>>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2 >>> f() 3

slide-131
SLIDE 131

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

Interactive Diagram >>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2 >>> f() 3

slide-132
SLIDE 132

Mutable Default Arguments are Dangerous

A default argument value is part of a function value, not generated by a call

16

Interactive Diagram >>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() 2 >>> f() 3 Each time the function is called, s is bound to the same value!