classes announcements for this lecture
play

Classes Announcements for This Lecture Assignments Lab this Week - PowerPoint PPT Presentation

Lecture 17 Classes Announcements for This Lecture Assignments Lab this Week A4 Thursday at midnight More prelim exercises Hopefully you are on Task 4 This time for-loops Minor extension for reasons Also tables, dictionaries


  1. Lecture 17 Classes

  2. Announcements for This Lecture Assignments Lab this Week • A4 Thursday at midnight • More prelim exercises § Hopefully you are on Task 4 § This time for-loops § Minor extension for reasons § Also tables, dictionaries • Will post A5 on Thursday Exams § Written assignment like A2 § Needs material from Tues • Last week for regrades • Will post A6 on Nov 3. § Limit them to valid issues § Not due until November 20 • Getting closer to prelim 2 § Want to avoid exam crunch 10/29/19 Classes 2

  3. Recall: Objects as Data in Folders • An object is like a manila folder Unique tab identifier • It contains other variables § Variables are called attributes id2 § Can change values of an attribute (with assignment statements) 2.0 x • It has a “tab” that identifies it 3.0 y § Unique number assigned by Python § Fixed for lifetime of the object 5.0 z 10/29/19 Classes 3

  4. Recall: Classes are Types for Objects • Values must have a type • Classes are how we add new types to Python § An object is a value § A class is its type id2 Types Point3 • int Classes 2.0 x • float class name • Point3 • bool • RGB 3.0 y • str • Turtle • Window 5.0 z 10/29/19 Classes 4

  5. Recall: Classes are Types for Objects • Values must have a type • Classes are how we add new types to Python § An object is a value § A class is its type id2 Types In Python3, type and class Point3 • int Classes are now both synonyms 2.0 x • float class name • Point3 • bool • RGB 3.0 y • str • Turtle • Window 5.0 z 10/29/19 Classes 5

  6. Classes Have Folders Too Object Folders Class Folders • Separate for each instance • Data common to all instances id2 Point3 Point3 id3 x 2.0 Point3 ???? y 3.0 x 5.0 z 5.0 y 7.2 z -0.5 10/29/19 Classes 6

  7. Goes inside a The Class Definition module, just like a function definition. class < class-name > (object): """Class specification""" < function definitions > < assignment statements > < any other statements also allowed> Example class Example(object): """The simplest possible class.""" pass 10/29/19 Classes 7

  8. Goes inside a The Class Definition module, just like a function keyword class definition. Beginning of a class < class-name > (object): class definition Do not forget the colon! Specification """Class specification""" more on this later (similar to one for a function) < function definitions > to define …but not often used < assignment statements > methods < any other statements also allowed> to define Example attributes class Example(object): Python creates """The simplest possible class.""" after reading the pass class definition 10/29/19 Classes 8

  9. Recall: Constructors • Function to create new instances id2 e id2 § Function name == class name Example § Created for you automatically • Calling the constructor: § Makes a new object folder Will come § Initializes attributes Example back to this § Returns the id of the folder • By default, takes no arguments § e = Example() 10/29/19 Classes 9

  10. Instances and Attributes • Assignments add object attributes id2 e § <object>.<att> = <expression> id2 § Example : e.b = 42 Example • Assignments can add class attributes 42 b § <class>.<att> = <expression> § Example : Example.a = 29 • Objects can access class attributes Example § Example : print e.a § But assigning it creates object attribute 29 a § Example : e.a = 10 • Rule : check object first, then class 10/29/19 Classes 10

  11. Instances and Attributes • Assignments add object attributes id2 e § <object>.<att> = <expression> id2 § Example : e.b = 42 Not how Example usually done • Assignments can add class attributes 42 b § <class>.<att> = <expression> § Example : Example.a = 29 • Objects can access class attributes Example § Example : print e.a § But assigning it creates object attribute 29 a § Example : e.a = 10 • Rule : check object first, then class 10/29/19 Classes 11

  12. Instances and Attributes • Assignments add object attributes id2 e § <object>.<att> = <expression> id2 § Example : e.b = 42 Example • Assignments can add class attributes 42 b § <class>.<att> = <expression> 10 a § Example : Example.a = 29 • Objects can access class attributes Example § Example : print e.a § But assigning it creates object attribute 29 a § Example : e.a = 10 • Rule : check object first, then class 10/29/19 Classes 12

  13. Invariants • Properties of an attribute that must be true • Works like a precondition: § If invariant satisfied, object works properly § If not satisfied, object is “corrupted” • Examples : § Point3 class: all attributes must be floats § RGB class: all attributes must be ints in 0..255 • Purpose of the class specification 10/29/19 Classes 13

  14. The Class Specification class Worker(object): """A class representing a worker in a certain organization Instance has basic worker info, but no salary information. Attribute lname: The worker last name Invariant: lname is a string Attribute ssn: The Social Security number Invariant: ssn is an int in the range 0..999999999 Attribute boss: The worker's boss Invariant: boss is an instace of Worker, or None if no boss""" 10/29/19 Classes 14

  15. The Class Specification class Worker(object): Short """A class representing a worker in a certain organization summary Instance has basic worker info, but no salary information. More detail Description Attribute lname: The worker last name Invariant: lname is a string Invariant Attribute ssn: The Social Security number Invariant: ssn is an int in the range 0..999999999 Attribute boss: The worker's boss Invariant: boss is an instace of Worker, or None if no boss""" 10/29/19 Classes 15

  16. The Class Specification class Worker(object): """A class representing a worker in a certain organization Instance has basic worker info, but no salary information. Warning: New format this year. Attribute lname: The worker last name Old exams will be very different. Invariant: lname is a string Attribute ssn: The Social Security number Invariant: ssn is an int in the range 0..999999999 Attribute boss: The worker's boss Invariant: boss is an instace of Worker, or None if no boss""" 10/29/19 Classes 16

  17. Recall: Objects can have Methods • Object before the name is an implicit argument • Example : distance >>> p = Point3(0,0,0) # First point >>> q = Point3(1,0,0) # Second point >>> r = Point3(0,0,1) # Third point >>> p.distance(r) # Distance between p, r 1.0 >>> q.distance(r) # Distance between q, r 1.4142135623730951 10/29/19 Classes 17

  18. Method Definitions 1. class Point3(object): • Looks like a function def 2. """Class for points in 3d space § Indented inside class 3. Invariant: x is a float § First param is always self 4. Invariant y is a float § But otherwise the same 5. Invariant z is a float """ 6. def distance (self,q): • In a method call : 7. """Returns dist from self to q § One less argument in () 8. Precondition: q a Point3""" § Obj in front goes to self 9. assert type(q) == Point3 • Example : a.distance(b) 10. sqrdst = ((self.x-q.x)**2 + 11. (self.y-q.y)**2 + self q 12. (self.z-q.z)**2) 13. return math.sqrt(sqrdst) 10/29/19 Classes 18

  19. Methods Calls • Example : a.distance(b) 1. class Point3(object): 2. """Class for points in 3d space a id2 b id3 3. Invariant: x is a float 4. Invariant y is a float id2 id3 5. Invariant z is a float """ Point3 Point3 x 1.0 x 0.0 def distance (self,q): 6. y 2.0 y 3.0 7. """Returns dist from self to q z 3.0 z -1.0 8. Precondition: q a Point3""" 9. assert type(q) == Point3 10. sqrdst = ((self.x-q.x)**2 + 11. (self.y-q.y)**2 + 12. (self.z-q.z)**2) 13. return math.sqrt(sqrdst) 10/29/19 Classes 19

  20. Methods Calls • Example : a.distance(b) 1. class Point3(object): 2. """Class for points in 3d space a id2 b id3 3. Invariant: x is a float 4. Invariant y is a float id2 id3 5. Invariant z is a float """ Point3 Point3 x 1.0 x 0.0 6. def distance (self,q): y 2.0 y 3.0 7. """Returns dist from self to q z 3.0 z -1.0 8. Precondition: q a Point3""" 9. assert type(q) == Point3 distance 9 10. sqrdst = ((self.x-q.x)**2 + 11. (self.y-q.y)**2 + self id2 12. (self.z-q.z)**2) q id3 13. return math.sqrt(sqrdst) 10/29/19 Classes 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