objects thinking about assignment 2
play

Objects Thinking About Assignment 2 A2 : three color models id1 - PowerPoint PPT Presentation

Mini-Lecture 16 Objects Thinking About Assignment 2 A2 : three color models id1 rgb id1 list RGB : 3 ints 0 to 255 CMYK : 4 floats 0.0 to 100.0 0 128 HSV : 3 floats, mult. bounds 1 0 We could represent as lists 2 0


  1. Mini-Lecture 16 Objects

  2. Thinking About Assignment 2 • A2 : three color models id1 rgb id1 list § RGB : 3 ints 0 to 255 § CMYK : 4 floats 0.0 to 100.0 0 128 § HSV : 3 floats, mult. bounds 1 0 § We could represent as lists 2 0 • Can get really confusing id2 § Easy to mix-up models cmyk id2 list § Easy to go out of bounds 0 0.0 • We want custom types 1 100.0 § One for each color model 2 100.0 § Motivation for classes 3 0.0 10/3/18 Objects 2

  3. Classes are Customized Types • Classes are any type not • Values look like dicts already built-into Python § Represent as a folder § Variables are named Types id2 • int RGB • float Classes • bool 255 red • RGB • str class name • CMYK • list 128 green • dict • HSV 0 blue 10/3/18 Objects 3

  4. Classes are Customized Types • Classes are any type not • Values look like dicts already built-into Python § Represent as a folder § Variables are named Types Class values are id2 called objects • int RGB • float Classes • bool 255 red • RGB • str class name • CMYK • list 128 green • dict • HSV 0 blue 10/3/18 Objects 4

  5. Why Are They Better Than dicts? id2 id2 dict RGB 'red' red 255 255 'green' 128 green 128 'blue' 0 blue 0 • Can add new variables • Variables fixed (sort-of) • Does not check bounds • Possibly checks bounds of the content variables of the content variables 10/3/18 Objects 5

  6. Why Are They Better Than dicts? id2 id2 dict RGB 'red' red 255 255 Designed for the purpose of safety 'green' 128 green 128 'blue' 0 blue 0 • Can add new variables • Variables fixed (sort-of) • Does not check bounds • Possibly checks bounds of the content variables of the content variables 10/3/18 Objects 6

  7. Using Classes in Python • Modules provide classes § Import to use the class id1 § Will show contents later Point3 • Example : introcs 2.0 x § Color classes for A2: class name RGB , CMYK , HSV 3.0 y § Geometry classes: 5.0 z Point2 , Point3 • Will make our own later 10/3/18 Objects 7

  8. Constructor: Function to make Objects • How do we create objects? Variable § Other types have literals stores ID p id2 § Example : 1, 'abc' , true not object § No such thing for objects instantiated • Constructor Function : object id2 § Same name as the class Point3 § Example : Point3(0,0,0) § Makes an object (manila folder) 0.0 x § Returns folder ID as value 0.0 y • Example : p = Point3(0, 0, 0) § Creates a Point object 0.0 z § Stores object’s ID in p 10/3/18 Objects 8

  9. Constructors and Modules >>> import introcs Actually a p id2 big number Need to import module that has Point class. id2 >>> p = introcs.Point3(0,0,0) Point3 Constructor is function. 0.0 x Prefix w/ module name. 0.0 >>> id(p) y 0.0 z Shows the ID of p. 10/3/18 Objects 9

  10. Object Variables • Variable stores object name § Reference to the object p id2 q id2 § Reason for folder analogy • Assignment uses object name id2 § Example : q = p Point3 § Takes name from p 0.0 x § Puts the name in q § Does not make new folder! 0.0 y • Like we saw with lists 0.0 z § Reason for using folders 10/3/18 Objects 10

  11. Objects and Attributes • Attributes are variables p id3 that live inside of objects § Can use in expressions id3 § Can assign values to them Point3 • Access : <variable>.<attr> 1.0 x § Example : p.x § Look like module variables 2.0 y • Putting it all together 3.0 z § p = introcs.Point3(1,2,3) § p.x = p.y + p.z 10/3/18 Objects 11

  12. Objects and Attributes • Attributes are variables p id3 that live inside of objects § Can use in expressions id3 § Can assign values to them Point3 • Access : <variable>.<attr> x 5.0 1.0 x § Example : p.x § Look like module variables 2.0 y • Putting it all together 3.0 z § p = introcs.Point3(1,2,3) § p.x = p.y + p.z 10/3/18 Objects 12

  13. Exercise: Attribute Assignment • Recall, q gets name in p >>> p = cornell.Point3(0,0,0) p id4 q id4 >>> q = p • Execute the assignments: id4 >>> p.x = 5.6 Point3 >>> q.x = 7.4 0.0 x • What is value of p.x ? 0.0 y A: 5.6 B: 7.4 0.0 z C: id4 D: I don ’ t know 10/3/18 Objects 13

  14. Exercise: Attribute Assignment • Recall, q gets name in p >>> p = geom.Point3(0,0,0) p id4 q id4 >>> q = p • Execute the assignments: id4 >>> p.x = 5.6 Point3 >>> q.x = 7.4 x 0.0 5.6 x • What is value of p.x ? 0.0 y A: 5.6 B: 7.4 CORRECT 0.0 z C: id4 D: I don’t know 10/3/18 Objects 14

  15. Exercise: Attribute Assignment • Recall, q gets name in p >>> p = geom.Point3(0,0,0) p id4 q id4 >>> q = p • Execute the assignments: id4 >>> p.x = 5.6 Point3 >>> q.x = 7.4 x x 0.0 5.6 7.4 x • What is value of p.x ? 0.0 y A: 5.6 B: 7.4 CORRECT 0.0 z C: id4 D: I don’t know 10/3/18 Objects 15

  16. Methods: Functions Tied to Objects • Method : function tied to object p id3 § Method call looks like a function call preceded by a variable name: ⟨ variable ⟩ . ⟨ method ⟩ ( ⟨ arguments ⟩ ) id3 § Example : p.distance(q) Point3 5.0 x § Example : p.abs() # makes x,y,z ≥ 0 2.0 y • Object acts like an argument § Distance p to q: p.distance(q) 3.0 z § Distance x to y: x.distance(y) § Different objects, different values 10/3/18 Objects 16

  17. Strings Have Methods Too >>> from introcs import index_str, count >>> s = 'Hello' >>> index_str(s,'e') 2 >>> s.index('e') 2 >>> count_str(s,'l') 2 >>> s.count('l') 2 10/3/18 Objects 17

  18. Strings Have Methods Too >>> from introcs import index_str, count >>> s = 'Hello' >>> index_str(s,'e') 2 >>> s.index('e') 2 >>> count_str(s,'l') 2 >>> s.count('l') 2 10/3/18 Objects 18

  19. Strings Have Methods Too >>> from introcs import index_str, count >>> s = 'Hello' >>> index_str(s,'e') 2 Are Strings >>> s.index('e') 2 objects? >>> count_str(s,'l') 2 >>> s.count('l') 2 10/3/18 Objects 19

  20. Surprise: All Values are in Objects! • Including basic values x id5 § int , float , bool , str • Example : id5 >>> x = 2.5 float >>> id(x) 2.5 • But they are immutable § Contents cannot change § Distinction between value and identity is immaterial § So we can ignore the folder x 2.5 10/3/18 Objects 20

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