objects
play

Objects [Andersen, Gries, Lee, Marschner, Van Loan, White] Lecture - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 7 Objects [Andersen, Gries, Lee, Marschner, Van Loan, White] Lecture 7 Announcements Please check the end of the Lecture 6 slides (slides 25- 29) for many announcements:


  1. CS 1110: Introduction to Computing Using Python Lecture 7 Objects [Andersen, Gries, Lee, Marschner, Van Loan, White]

  2. Lecture 7 Announcements • Please check the end of the Lecture 6 slides (slides 25- 29) for many announcements: http://www.cs.cornell.edu/courses/cs1110/2017sp/lectures/02-14-17/presentation-06.pdf • Incorrect link for how to break up long lines in Section 10 of Assignment 1. Watch course website for announcements about A1: http://www.cs.cornell.edu/courses/cs1110/2017sp/announcements.php 2/16/17 Objects 2

  3. Review: Types • Type int : • Type str :  Values : integers  Values : string literals  Ops : +, –, *, /, %, ** • Double quotes: "abc" • Single quotes: 'abc' • Type float :  Ops : + (concatenation)  Values : real numbers  Ops : +, –, *, /, ** • Type bool :  Values : True and False  Ops : not, and, or 2/16/17 Objects 3

  4. Built-in Types are not “Enough” • 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  This can get really messy y • How about a single variable 5.0 z that represents a point? 2/16/17 Objects 4

  5. Built-in Types are not “Enough” • 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  This can get really messy y • How about a single variable 5.0 z that represents a point? 2/16/17 Objects 5

  6. 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 2/16/17 Objects 6

  7. Classes: Types for Objects • Values must have a type id1  An object is a value Point3  Object type is a class 2.0 x class name • Modules provide classes 3.0  Will show how later y • Example : geom 5.0 z  Classes: Point2 , Point3 2/16/17 Objects 7

  8. Classes: Types for Objects • Classes are how we add new types to Python • Sort of like a template Types Point3 • int Classes • float • Point3 Point3s have an x, y, and z • bool • Point2 • str Class 2/16/17 Objects 8

  9. Constructor: Function to make Objects • How do we create objects? • Constructor Function : new id (in this case id2 )  Format: ⟨ class name ⟩ ( ⟨ arguments ⟩ )  Example : Point3(0.0,0.0,0.0) instantiated  Makes a new object (manila folder) object id2 with a new id  Called an instantiated object Point3  Returns folder id as value 0.0 x Point3 0.0 y Point3s have an x, y, and z 0.0 z 2/16/17 Objects 9

  10. Constructor: Function to make Objects • How do we create objects? Variable • Constructor Function : stores ID p id2  Format: ⟨ class name ⟩ ( ⟨ arguments ⟩ ) not object  Example : Point3(0.0,0.0,0.0) instantiated  Makes a new object (manila folder) object id2 with a new id  Called an instantiated object Point3  Returns folder id as value 0.0 x • Example : p = Point3(0.0, 0.0, 0.0)  Creates a Point object 0.0 y  Stores object’s id in p 0.0 z Like a Greek god! 2/16/17 Objects 10

  11. Constructors and Modules >>> import geom p id2 Need to import module that has Point class. id2 >>> p = geom.Point3(0.0,0.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. 2/16/17 Objects 11

  12. Accessing Attributes • Attributes are variables p id3 that live inside of objects  Can use in expressions id3  Can assign values to them Point3 • Format : ⟨ variable ⟩ . ⟨ attribute ⟩ 1.0 x  Example : p.x 2.0  Look like module variables y • To evaluate p.x , Python: 3.0 z 1. finds folder with id stored in p 2. returns the value of x in that folder 2/16/17 Objects 12

  13. Accessing Attributes • Example : p id3  p = geom.Point3(1.0, 2.0, 3.0) id3  p.x = p.y + p.z Point3 x 1.0 5.0 x 2.0 y 3.0 z 2/16/17 Objects 13

  14. Object Variables • Variable stores object id  Reference to the object disliked id2 liked id2  Reason for folder analogy • Assignment uses object id id2  Example : liked = disliked Point3  Takes contents from disliked 0.0  Puts contents in liked x  Does not make new folder! 0.0 y • This is the cause of many 0.0 z mistakes in this course 2/16/17 Objects 14

  15. Exercise: Attribute Assignment >>> 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 • What is value of p.x ? 0.0 x 0.0 y A: 5.6 B: 7.4 CORRECT 0.0 z C: id4 D: I don’t know 2/16/17 Objects 15

  16. Exercise: Attribute Assignment >>> 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 • What is value of p.x ? 0.0 5.6 x 0.0 y A: 5.6 B: 7.4 CORRECT 0.0 z C: id4 D: I don’t know 2/16/17 Objects 16

  17. Exercise: Attribute Assignment >>> 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 • What is value of p.x ? 0.0 5.6 7.4 x 0.0 y A: 5.6 B: 7.4 CORRECT 0.0 z C: id4 D: I don’t know 2/16/17 Objects 17

  18. Assignment and Attribute Oddness >>> from geom import * >>> p = 5.0 >>> p = Point3(1.0,2.0,3.0) >>> q = p >>> q = p >>> p = 4.0 >>> p.x = 4.0 >>> q >>> q.x 5.0 4.0 !! The rules of variables have not changed! However, combining variable assignment with object references can be confusing. 2/16/17 Objects 18

  19. Call Frames and Objects Global STUFF • Objects can be altered in a function call id5 p id5  Object variables hold id s! Point3  Folder can be accessed from x 1.0 global variable or parameter … • Example : Call Frame def incr_x(q): incr_x 1 q.x = q.x + 1.0 1 q id5 >>> p = geom.Point3(1.0, 2.0, 3.0) >>> incr_x(p) 2/16/17 Objects 19

  20. Call Frames and Objects Global STUFF • Objects can be altered in a function call id5 p id5  Object variables hold id s! Point3  Folder can be accessed from x x 1.0 2.0 global variable or parameter … • Example : Call Frame def incr_x(q): incr_x q.x = q.x + 1.0 1 q id5 >>> p = geom.Point3(1.0, 2.0, 3.0) >>> incr_x(p) 2/16/17 Objects 20

  21. Call Frames and Objects Global STUFF • Objects can be altered in a function call id5 p id5  Object variables hold id s! Point3  Folder can be accessed from x x 1.0 2.0 global variable or parameter … • Example : Call Frame def incr_x(q): q.x = q.x + 1.0 1 >>> p = geom.Point3(1.0, 2.0, 3.0) >>> incr_x(p) 2/16/17 Objects 21

  22. Exercise: Attribute Assignment import geom Draw everything that gets created. p = geom.Point3(1.0,2.0,3.0) How many folders get drawn? q = geom.Point3(3.0,4.0,5.0) 2/16/17 2/16/17 Objects 22

  23. Exercise: Attribute Assignment import geom Draw everything that gets created. p = geom.Point3(1.0,2.0,3.0) How many folders get drawn? q = geom.Point3(3.0,4.0,5.0) id1 id2 Point3 Point3 x 1.0 x 3.0 y 2.0 y 4.0 z 3.0 z 5.0 2/16/17 2/16/17 Objects 23

  24. Exercise: Attribute Assignment import geom Draw everything that gets created. p = geom.Point3(1.0,2.0,3.0) How many folders get drawn? q = geom.Point3(3.0,4.0,5.0) What else gets drawn? id1 id2 Point3 Point3 x 1.0 x 3.0 y 2.0 y 4.0 z 3.0 z 5.0 2/16/17 2/16/17 Objects 24

  25. Exercise: Attribute Assignment import geom Draw everything that gets created. p = geom.Point3(1.0,2.0,3.0) How many folders get drawn? q = geom.Point3(3.0,4.0,5.0) What else gets drawn? p id1 q id2 id1 id2 Point3 Point3 x 1.0 x 3.0 y 2.0 y 4.0 z 3.0 z 5.0 2/16/17 2/16/17 Objects 25

  26. Exercise: Attribute Assignment import geom def swap_x(p, q): p = geom.Point3(1.0,2.0,3.0) t = p.x 1 q = geom.Point3(3.0,4.0,5.0) p.x = q.x 2 swap_x(p, q) q.x = t 3 Execute swap_x on what we just drew. There should be a call frame. What is in p.x at the end? A: 1.0 B: 2.0 C: 3.0 D: I don’t know 2/16/17 Objects 26

  27. Exercise: Attribute Assignment import geom def swap_x(p, q): p = geom.Point3(1.0,2.0,3.0) t = p.x 1 q = geom.Point3(3.0,4.0,5.0) p.x = q.x 2 swap_x(p, q) q.x = t 3 p id1 q id2 id1 id2 swap_x: 1 swap_x Point3 Point3 p p q q x 1.0 x 3.0 t y 2.0 y 4.0 z 3.0 z 5.0 2/16/17 Objects 27

  28. Exercise: Attribute Assignment import geom def swap_x(p, q): p = geom.Point3(1.0,2.0,3.0) t = p.x 1 q = geom.Point3(3.0,4.0,5.0) p.x = q.x 2 swap_x(p, q) q.x = t 3 p id1 q id2 id1 id2 swap_x swap_x: 1 1 Point3 Point3 p p q q id1 id2 x 1.0 x 3.0 t y 2.0 y 4.0 z 3.0 z 5.0 2/16/17 Objects 28

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