mutable values announcements objects
play

Mutable Values Announcements Objects (Demo) Objects 4 Objects - PowerPoint PPT Presentation

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


  1. 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 • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg LATIN CAPITAL LETTER A ' ⚅ ' ' ♪ ' DIE FACE-6 EIGHTH NOTE 7

  2. 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 • 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

  3. Mutation Operations

  4. Some Objects Can Change [Demo] 9

  5. Some Objects Can Change [Demo] First example in the course of an object changing state 9

  6. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 9

  7. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👷 same_person 9

  8. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👷 same_person BABY 9

  9. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👷 same_person Unicode character BABY name 9

  10. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👨 same_person Unicode character GIRL name 9

  11. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👨 jessica same_person Unicode character GIRL name 9

  12. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👪 jessica same_person Unicode character WOMAN name 9

  13. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👶 jessica same_person Unicode character OLDER name WOMAN 9

  14. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👶 jessica same_person Unicode character OLDER name WOMAN All names that refer to the same object are affected by a mutation 9

  15. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👶 jessica same_person Unicode character OLDER name WOMAN All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries 9

  16. Some Objects Can Change [Demo] First example in the course of an object changing state The same object can change in value throughout the course of computation 👶 jessica same_person Unicode character OLDER name WOMAN All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries {Demo} 9

  17. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope 10

  18. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope >>> four = [1, 2, 3, 4] 10

  19. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope >>> four = [1, 2, 3, 4] >>> len(four) 4 10

  20. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) 10

  21. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> mystery(four) >>> len(four) 2 10

  22. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope >>> four = [1, 2, 3, 4] def mystery(s): >>> len(four) s.pop() 4 s.pop() >>> mystery(four) >>> len(four) 2 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

  23. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] 4 s.pop() >>> mystery(four) >>> len(four) 2 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

  24. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] 4 s.pop() >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] 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

  25. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] 4 s.pop() >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 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

  26. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] 4 s.pop() >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! 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

  27. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] 4 s.pop() >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] >>> len(four) 4 >>> another_mystery() # No arguments! >>> len(four) 2 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

  28. Mutation Can Happen Within a Function Call A function can change the value of any object in its scope or >>> four = [1, 2, 3, 4] def mystery(s): def mystery(s): >>> len(four) s.pop() s[2:] = [] 4 s.pop() >>> mystery(four) >>> len(four) 2 >>> four = [1, 2, 3, 4] def another_mystery(): >>> len(four) four.pop() 4 four.pop() >>> another_mystery() # No arguments! >>> len(four) 2 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

  29. Tuples (Demo)

  30. Tuples are Immutable Sequences 12

  31. Tuples are Immutable Sequences Immutable values are protected from mutation 12

  32. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) 12

  33. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> ooze() 12

  34. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> ooze() >>> turtle 12

  35. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> ooze() >>> turtle (1, 2, 3) 12

  36. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> turtle (1, 2, 3) 12

  37. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> ooze() >>> turtle (1, 2, 3) 12

  38. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> ooze() >>> turtle >>> turtle (1, 2, 3) 12

  39. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> ooze() >>> turtle >>> turtle (1, 2, 3) ['Anything could be inside!'] 12

  40. 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!'] 12

  41. 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 12

  42. 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 Name change: 12

  43. 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 >>> x + x Name change: >>> x + x 12

  44. 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 >>> x = 2 >>> x + x Name change: >>> x + x 12

  45. 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 >>> x = 2 >>> x + x 4 Name change: >>> x + x 12

  46. 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 >>> x = 2 >>> x + x 4 Name change: >>> x = 3 >>> x + x 12

  47. 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 >>> x = 2 >>> x + x 4 Name change: >>> x = 3 >>> x + x 6 12

  48. 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 >>> x = 2 >>> x + x 4 Name change: Object mutation: >>> x = 3 >>> x + x 6 12

  49. 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 >>> x = 2 >>> x + x >>> x + x 4 Name change: Object mutation: >>> x = 3 >>> x + x >>> x + x 6 12

  50. 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 >>> x = 2 >>> x = [1, 2] >>> x + x >>> x + x 4 Name change: Object mutation: >>> x = 3 >>> x + x >>> x + x 6 12

  51. 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 >>> x = 2 >>> x = [1, 2] >>> x + x >>> x + x 4 [1, 2, 1, 2] Name change: Object mutation: >>> x = 3 >>> x + x >>> x + x 6 12

  52. 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 >>> 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 12

  53. 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 >>> 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] 12

  54. 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 >>> 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 12

  55. 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 >>> 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) 12

  56. 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 >>> 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[0] = 4 12

  57. 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 >>> 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[0] = 4 ERROR 12

  58. 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 >>> 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 ERROR 12

  59. 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 >>> 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 ERROR 12

  60. 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 >>> 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 ERROR >>> s 12

  61. 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 >>> 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 ERROR >>> s ([4, 2], 3) 12

  62. Mutation

  63. Sameness and Change 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend