Lecture 9 Objects
Announcements for Today Assignment 1 Assignment 2 • We are starting grading • Posted Today § Will take most of the day § Written assignment § Grades 5pm tomorrow § Do while revising A1 • Resubmit until correct § Relatively short § Read feedback in CMS • Due next Tuesday § Reupload/request regrade § Submit as a PDF • If you were very wrong … § Scan or phone picture § You got an e-mail § More 1-on-1s this week 9/20/18 Objects 2
Type: Set of values and the operations on them • Type int : • Type str : § Values : integers § Values : string literals • Double quotes: "abc" § Ops : +, –, *, //, %, ** • Single quotes: 'abc' • Type float : § Ops : + (concatenation) § Values : real numbers § Ops : +, –, *, /, ** • Type bool : Are the the only § Values : True and False types that exist? § Ops : not, and, or 9/20/18 Objects 3
Type: Set of values and the operations on them • Want a point in 3D space § We need three variables § x , y , z coordinates • What if have a lot of points? § Vars x0, y0, z0 for first point § Vars x1, y1, z1 for next point 2.0 x § … 3.0 y § This can get really messy • How about a single variable 5.0 z that represents a point? 9/20/18 Objects 4
Type: Set of values and the operations on them • Want a point in 3D space • Can we stick them together in a � folder � ? § We need three variables § x , y , z coordinates • Motivation for objects • What if have a lot of points? § Vars x0, y0, z0 for first point § Vars x1, y1, z1 for next point 2.0 x § … 3.0 y § This can get really messy • How about a single variable 5.0 z that represents a point? 9/20/18 Objects 5
Objects: Organizing Data in Folders • An object is like a manila folder Unique tab identifier • It contains other variables § Variables are called attributes § These values can change id1 • It has an ID that identifies it 2.0 x § Unique number assigned by Python 3.0 y (just like a NetID for a Cornellian) § Cannot ever change 5.0 z § Has no meaning; only identifies 9/20/18 Objects 6
Classes: Types for Objects • Values must have a type § An object is a value id1 § Type of object is its class Point3 • Modules provide classes 2.0 x § Will show how later class name 3.0 • Example : introcs y § Part of CornellExtensions 5.0 z § Just need to import it § Classes: Point2 , Point3 9/20/18 Objects 7
The Old Way: Classes vs Types • Values must have a type • Classes are how we add new types to Python § An object is a value § Object type is a class Types id2 Point3 • int Classes 2.0 x • float class name • Point3 • bool • Point2 3.0 y • str • Window 5.0 z 9/20/18 Objects 8
The Old Way: Classes vs Types • Values must have a type • Classes are how we add new types to Python § An object is a value § Object type is a class Types id2 But in Python3, type and class Point3 • int Classes are now both synonyms 2.0 x • float class name • Point3 • bool • Point2 3.0 y • str • Window 5.0 z 9/20/18 Objects 9
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 9/20/18 Objects 10
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. 9/20/18 Objects 11
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 • This is the cause of many 0.0 z mistakes in this course 9/20/18 Objects 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> 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 9/20/18 Objects 13
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 1.0 5.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 9/20/18 Objects 14
Exercise: Attribute Assignment • Recall, q gets name in p >>> p = introcs.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 9/20/18 Objects 15
Exercise: Attribute Assignment • Recall, q gets name in p >>> p = introcs.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 9/20/18 Objects 16
Exercise: Attribute Assignment • Recall, q gets name in p >>> p = introcs.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 9/20/18 Objects 17
Call Frames and Objects Global STUFF • Mutable objects can be altered in a function call id5 p id5 § Object vars hold names! Point3 § Folder accessed by both x 0.0 global var & parameter … • Example : Call Frame def incr_x(q): incr_x 1 q.x = q.x + 1 1 q id5 >>> p = introcs.Point3(0,0,0) >>> incr_x(p) 9/20/18 Objects 18
Call Frames and Objects Global STUFF • Mutable objects can be altered in a function call id5 p id5 § Object vars hold names! Point3 § Folder accessed by both x 1.0 x 0.0 global var & parameter … • Example : Call Frame def incr_x(q): incr_x q.x = q.x + 1 1 q id5 >>> p = introcs.Point3(0,0,0) >>> incr_x(p) 9/20/18 Objects 19
Call Frames and Objects Global STUFF • Mutable objects can be altered in a function call id5 p id5 § Object vars hold names! Point3 § Folder accessed by both x 1.0 x 0.0 global var & parameter … • Example : Call Frame def incr_x(q): q.x = q.x + 1 1 >>> p = introcs.Point3(0,0,0) >>> incr_x(p) 9/20/18 Objects 20
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 • Just like we saw for strings § s = 'abracadabra' 3.0 z § s.index('a') • Are strings objects? 9/20/18 Objects 21
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 9/20/18 Objects 22
Surprise: All Values are in Objects! • Including basic values x id6 § int , float , bool , str • Example : id6 s g n i r t s s e d >>> x = 'foo' u l c str n i >>> id(x) 'foo' • But they are immutable § No string method can alter the contents of a string § x.replace('o','y') evaluates to 'fyy' but x is still 'foo' § So we can ignore the folder 'foo' x 9/20/18 Objects 23
Class Objects Example : Files • Use name class object to f id6 distinguish from other values id6 § Not int , float , bool , str file • Class objects are mutable § You can change them name, position, § Methods can have effects state, … besides their return value • Example : f = open('jabber.txt') § p = Point(3,-3,0) s = f.read() § p.clamp(-1,1) f.close() Opens a file on your disk; returns a file object you can read 9/20/18 Objects 24
Base Types vs. Classes Base Types Classes • Built-into Python • Provided by modules • Refer to instances as values • Refer to instances as objects • Instantiate with literals • Instantiate w/ constructors • Are all immutable • Can alter attributes • Can ignore the folders • Must represent with folders 9/20/18 Objects 25
Recommend
More recommend