die face 6 eighth note demo 7 some objects can change
play

' ' ' ' DIE FACE-6 EIGHTH NOTE (Demo) 7 Some Objects Can - PDF document

Announcements Mutable Values 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


  1. Announcements Mutable Values 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: Objects • 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 (Demo) • Functions do one thing; objects do many related things 4 Representing Strings: the ASCII Standard American Standard Code for Information Interchange "Bell" (\a) "Line feed" (\n) 0 0 0 8 rows: 3 bits 0 0 1 0 1 0 Example: Strings 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 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 (Demo) • Control characters were designed for transmission (Demo) 6 Representing Strings: the Unicode Standard • 137,994 characters in Unicode 12.1 • 150 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order Mutation Operations • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg LATIN CAPITAL LETTER A ' ⚅ ' ' ♪ ' DIE FACE-6 EIGHTH NOTE (Demo) 7

  2. Some Objects Can Change Mutation Can Happen Within a Function Call [Demo] A function can change the value of any object in its scope First example in the course of an object changing state or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] The same object can change in value throughout the course of computation 4 s.pop() >>> mystery(four) 👷 👨 👶 👪 >>> len(four) jessica 2 same_person Unicode character OLDER WOMAN BABY GIRL >>> four = [1, 2, 3, 4] def another_mystery(): name WOMAN >>> len(four) four.pop() 4 four.pop() All names that refer to the same object are affected by a mutation >>> another_mystery() # No arguments! >>> len(four) 2 Only objects of mutable types can change: lists & dictionaries {Demo} 9 10 pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0 Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> ooze() Next lecture: ooze can >>> turtle >>> turtle change turtle's binding (1, 2, 3) ['Anything could be inside!'] The value of an expression can change because of changes in names or objects Tuples >>> x = 2 >>> x = [1, 2] >>> x + x >>> x + x 4 [1, 2, 1, 2] Name change: Object mutation: >>> x = 3 >>> x.append(3) >>> x + x >>> x + x 6 [1, 2, 3, 1, 2, 3] An immutable sequence may still change if it contains a mutable value as an element >>> s = ([1, 2], 3) >>> s = ([1, 2], 3) >>> s[0] = 4 >>> s[0][0] = 4 (Demo) ERROR >>> s ([4, 2], 3) 12 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 Mutation • Conversely, we could have two lists that happen to have the same contents, but are different >>> a = [10] >>> a = [10] >>> b = a >>> b = [10] >>> a == b >>> a == b True True >>> a.append(20) >>> b.append(20) >>> a >>> a [10, 20] [10] >>> b >>> b [10, 20] [10, 20] >>> a == b >>> a == b True False 14 Identity Operators Mutable Default Arguments are Dangerous Identity A default argument value is part of a function value, not generated by a call <exp0> is <exp1> >>> def f(s=[]): ... s.append(3) ... return len(s) evaluates to True if both <exp0> and <exp1> evaluate to the same object ... >>> f() 1 Equality >>> f() 2 Each time the function <exp0> == <exp1> >>> f() is called, s is bound to the same value! 3 evaluates to True if both <exp0> and <exp1> evaluate to equal values Identical objects are always equal values (Demo) 15 16 pythontutor.com/composingprograms.html#code=def%20f%28s%3D[]%29%3A%0A%20%20%20%20s.append%283%29%0A%20%20%20%20return%20len%28s%29%0A%20%20%20%20%0Af%28%29%0Af%28%29%0Af%28%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0

  3. Lists in Environment Diagrams Assume that before each example below we execute: s = [2, 3] t = [5, 6] Operation Example Result Global list s append adds one s.append(t) s → [2, 3, [5, 6]] 0 1 2 2 3 element to a list t = 0 t → 0 0 t 2 3 5 6 extend adds all s.extend(t) s → [2, 3, 5, 6] Lists elements in one list t[1] = 0 t → [5, 0] a to another list b list list 0 0 1 addition & slicing a = s + [t] s → [2, 3] 5 6 0 0 create new lists b = a[1:] t → [5, 0] containing existing a[1] = 9 a → [2, 9, [5, 0]] elements b[1][1] = 0 b → [3, [5, 0]] list 0 1 2 2 3 9 list 0 1 3 18 Lists in Environment Diagrams Lists in Environment Diagrams Assume that before each example below we execute: Assume that before each example below we execute: s = [2, 3] s = [2, 3] t = [5, 6] t = [5, 6] Operation Example Result Global Operation Example Result Global list list 0 1 append adds one s.append(t) s → [2, 3, [5, 6]] s append adds one s.append(t) s → [2, 3, [5, 6]] s 2 3 0 1 element to a list t = 0 t → 0 element to a list t = 0 t → 0 t t 2 3 0 extend adds all s.extend(t) s → [2, 3, 5, 6] extend adds all s.extend(t) s → [2, 3, 5, 6] elements in one list t[1] = 0 t → [5, 0] elements in one list t[1] = 0 t → [5, 0] list to another list to another list 0 1 2 3 addition & slicing a = s + [t] s → [2, 3] addition & slicing a = s + [t] s → [2, 3] create new lists b = a[1:] t → [5, 0] create new lists b = a[1:] t → [5, 0] containing existing a[1] = 9 a → [2, 9, [5, 0]] containing existing a[1] = 9 a → [2, 9, [5, 0]] list list elements b[1][1] = 0 b → [3, [5, 0]] elements b[1][1] = 0 b → [3, [5, 0]] 1 1 0 0 s → [2, 0] 5 6 5 6 The list function t = list(s) The list function t = list(s) s → [2, 0] also creates a new s[1] = 0 t → [2, 3] also creates a new s[1] = 0 t → [2, 3] list containing list containing existing elements existing elements slice assignment s[0:0] = t replaces a slice with s[3:] = t new values t[1] = 0 19 20 Lists in Environment Diagrams Lists in Environment Diagrams Assume that before each example below we execute: Assume that before each example below we execute: s = [2, 3] s = [2, 3] t = [5, 6] t = [5, 6] Operation Example Result Global Operation Example Result list 0 1 2 3 4 append adds one s.append(t) s → [2, 3, [5, 6]] s pop removes & returns t = s.pop() s → [2] 5 6 2 3 5 6 element to a list t = 0 t → 0 t the last element t → 3 extend adds all s.extend(t) s → [2, 3, 5, 6] remove removes the t.extend(t) s → [2, 3] elements in one list t[1] = 0 t → [5, 0] first element equal t.remove(5) t → [6, 5, 6] to another list to the argument addition & slicing a = s + [t] s → [2, 3] slice assignment can s[:1] = [] s → [3] create new lists b = a[1:] t → [5, 0] remove elements from t[0:2] = [] t → [] containing existing a[1] = 9 a → [2, 9, [5, 0]] list a list by assigning elements b[1][1] = 0 b → [3, [5, 0]] 0 1 [] to a slice. 5 6 0 The list function t = list(s) s → [2, 0] also creates a new s[1] = 0 t → [2, 3] list containing existing elements slice assignment s[0:0] = t s → [5, 6, 2, 5, 6] replaces a slice with s[3:] = t t → [5, 0] new values t[1] = 0 21 22 Lists in Lists in Lists in Environment Diagrams t = [1, 2, 3] list Global t[1:3] = [t] 0 1 2 1 2 3 t 1 2 3 1 t.extend(t) list 0 [t] evaluates to: [1, [...], 1, [...]] t = [[1, 2], [3, 4]] list list Global t[0].append(t[1:2]) 0 1 0 1 3 4 t list list 0 1 2 0 1 2 [[1, 2, [[3, 4]]], [3, 4]] 23

Recommend


More recommend