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 • 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recommend
More recommend