61a lecture 12 announcements objects
play

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


  1. Representing Strings: the Unicode Standard • 109,000 characters • 93 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 U+0058 LATIN CAPITAL LETTER X ' ☺ ' ' ☹ ' U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE (Demo) 7

  2. Mutation Operations

  3. Some Objects Can Change [Demo] 9

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

  5. 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

  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 👷 same_person 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 BABY 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 Unicode character BABY name 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 GIRL 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 👨 jessica 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 WOMAN 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 OLDER 
 name WOMAN 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 All names that refer to the same object are affected by a mutation 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 Only objects of mutable types can change: lists & dictionaries 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 {Demo} 9

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

  17. 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

  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] >>> len(four) 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 >>> mystery(four) 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) >>> len(four) 2 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] def mystery(s): >>> len(four) s.pop() 4 s.pop() >>> mystery(four) >>> len(four) 2 10 Interactive Diagram

  22. 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 Interactive Diagram

  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 >>> four = [1, 2, 3, 4] 10 Interactive Diagram

  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] >>> len(four) 4 10 Interactive Diagram

  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 >>> another_mystery() # No arguments! 10 Interactive Diagram

  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! >>> len(four) 2 10 Interactive Diagram

  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] def another_mystery(): >>> len(four) four.pop() 4 four.pop() >>> another_mystery() # No arguments! >>> len(four) 2 10 Interactive Diagram

  28. Tuples (Demo)

  29. Tuples are Immutable Sequences 12

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

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

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

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

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

  35. Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> 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() >>> 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 >>> 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) ['Anything could be inside!'] 12

  39. 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

  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!'] The value of an expression can change because of changes in names or objects 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 Name change: 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 >>> x + x Name change: >>> x + x 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 = 2 >>> 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 4 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 = 3 >>> 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 6 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: Object mutation: >>> 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 >>> x + x 4 Name change: Object mutation: >>> x = 3 >>> x + x >>> 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 = [1, 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 [1, 2, 1, 2] 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.append(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 [1, 2, 3, 1, 2, 3] 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] An immutable sequence may still change if it contains a mutable value as an element 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 >>> s = ([1, 2], 3) 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) >>> s[0] = 4 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 ERROR 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 = ([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 >>> s[0][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 >>> s 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 ([4, 2], 3) 12

  61. Mutation

  62. Sameness and Change 14

  63. Sameness and Change • As long as we never modify objects, a compound object is just the totality of its pieces 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