Objects Announcements for Today Assignment 1 Assignment 2 We are - - PowerPoint PPT Presentation

objects announcements for today
SMART_READER_LITE
LIVE PREVIEW

Objects Announcements for Today Assignment 1 Assignment 2 We are - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Objects

Lecture 9

slide-2
SLIDE 2

Announcements for Today

Assignment 1

  • We are starting grading

§ Will take most of the day § Grades 5pm tomorrow

  • Resubmit until correct

§ Read feedback in CMS § Reupload/request regrade

  • If you were very wrong…

§ You got an e-mail § More 1-on-1s this week

Assignment 2

  • Posted Today

§ Written assignment § Do while revising A1 § Relatively short

  • Due next Tuesday

§ Submit as a PDF § Scan or phone picture

9/20/18 Objects 2

slide-3
SLIDE 3

Type: Set of values and the operations on them

  • Type int:

§ Values: integers § Ops: +, –, *, //, %, **

  • Type float:

§ Values: real numbers § Ops: +, –, *, /, **

  • Type bool:

§ Values: True and False § Ops: not, and, or

  • Type str:

§ Values: string literals

  • Double quotes: "abc"
  • Single quotes: 'abc'

§ Ops: + (concatenation)

9/20/18 Objects 3

Are the the only types that exist?

slide-4
SLIDE 4

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 § … § This can get really messy

  • How about a single variable

that represents a point?

9/20/18 Objects 4

x 2.0 y 3.0 z 5.0

slide-5
SLIDE 5
  • Can we stick them

together in a folder?

  • Motivation for objects

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 § … § This can get really messy

  • How about a single variable

that represents a point?

9/20/18 Objects 5

x 2.0 y 3.0 z 5.0

slide-6
SLIDE 6

Objects: Organizing Data in Folders

  • An object is like a manila folder
  • It contains other variables

§ Variables are called attributes § These values can change

  • It has an ID that identifies it

§ Unique number assigned by Python (just like a NetID for a Cornellian) § Cannot ever change § Has no meaning; only identifies

9/20/18 Objects 6

id1 x 2.0 y 3.0 z 5.0 Unique tab identifier

slide-7
SLIDE 7

Classes: Types for Objects

  • Values must have a type

§ An object is a value § Type of object is its class

  • Modules provide classes

§ Will show how later

  • Example: introcs

§ Part of CornellExtensions § Just need to import it § Classes: Point2, Point3

9/20/18 Objects 7

id1 x 2.0 y 3.0 z 5.0

Point3

class name

slide-8
SLIDE 8

The Old Way: Classes vs Types

  • Values must have a type

§ An object is a value § Object type is a class

  • Classes are how we add

new types to Python

9/20/18 8

id2 x 2.0 y 3.0 z 5.0

Point3

class name Classes

  • Point3
  • Point2
  • Window

Types

  • int
  • float
  • bool
  • str

Objects

slide-9
SLIDE 9

The Old Way: Classes vs Types

  • Values must have a type

§ An object is a value § Object type is a class

  • Classes are how we add

new types to Python

9/20/18 9

id2 x 2.0 y 3.0 z 5.0

Point3

class name Classes

  • Point3
  • Point2
  • Window

Types

  • int
  • float
  • bool
  • str

Objects

But in Python3, type and class are now both synonyms

slide-10
SLIDE 10

Constructor: Function to make Objects

  • How do we create objects?

§ Other types have literals § Example: 1, 'abc', true § No such thing for objects

  • Constructor Function:

§ Same name as the class § Example: Point3(0,0,0) § Makes an object (manila folder) § Returns folder ID as value

  • Example: p = Point3(0, 0, 0)

§ Creates a Point object § Stores object’s ID in p

9/20/18 Objects 10

id2 p Variable stores ID not object instantiated

  • bject

id2 x 0.0 y 0.0 z 0.0

Point3

slide-11
SLIDE 11

Constructors and Modules

>>> import introcs >>> p = introcs.Point3(0,0,0) >>> id(p)

9/20/18 Objects 11

id2 p id2 x 0.0 y 0.0 z 0.0

Point3

Need to import module that has Point class. Constructor is function. Prefix w/ module name. Shows the ID of p.

Actually a big number

slide-12
SLIDE 12

Object Variables

  • Variable stores object name

§ Reference to the object § Reason for folder analogy

  • Assignment uses object name

§ Example: q = p § Takes name from p § Puts the name in q § Does not make new folder!

  • This is the cause of many

mistakes in this course

9/20/18 Objects 12

id2 p id2 x 0.0 y 0.0 z 0.0

Point3

id2 q

slide-13
SLIDE 13

Objects and Attributes

  • Attributes are variables

that live inside of objects

§ Can use in expressions § Can assign values to them

  • Access: <variable>.<attr>

§ Example: p.x § Look like module variables

  • Putting it all together

§ p = introcs.Point3(1,2,3) § p.x = p.y + p.z

9/20/18 Objects 13

id3 x 1.0 y 2.0 z 3.0 id3 p

Point3

slide-14
SLIDE 14

Objects and Attributes

  • Attributes are variables

that live inside of objects

§ Can use in expressions § Can assign values to them

  • Access: <variable>.<attr>

§ Example: p.x § Look like module variables

  • Putting it all together

§ p = introcs.Point3(1,2,3) § p.x = p.y + p.z

9/20/18 Objects 14

id3 x 1.0 y 2.0 z 3.0 id3 p

Point3

5.0

x

slide-15
SLIDE 15

Exercise: Attribute Assignment

  • Recall, q gets name in p

>>> p = introcs.Point3(0,0,0) >>> q = p

  • Execute the assignments:

>>> p.x = 5.6 >>> q.x = 7.4

  • What is value of p.x?

9/20/18 Objects 15

id4 p id4 q A: 5.6 B: 7.4 C: id4 D: I don’t know id4 x 0.0 y 0.0 z 0.0

Point3

slide-16
SLIDE 16

Exercise: Attribute Assignment

  • Recall, q gets name in p

>>> p = introcs.Point3(0,0,0) >>> q = p

  • Execute the assignments:

>>> p.x = 5.6 >>> q.x = 7.4

  • What is value of p.x?

9/20/18 Objects 16

id4 p id4 q A: 5.6 B: 7.4 C: id4 D: I don’t know id4 x 0.0 y 0.0 z 0.0

Point3

5.6 CORRECT

x

slide-17
SLIDE 17

Exercise: Attribute Assignment

  • Recall, q gets name in p

>>> p = introcs.Point3(0,0,0) >>> q = p

  • Execute the assignments:

>>> p.x = 5.6 >>> q.x = 7.4

  • What is value of p.x?

9/20/18 Objects 17

id4 p id4 q A: 5.6 B: 7.4 C: id4 D: I don’t know id4 x 0.0 y 0.0 z 0.0

Point3

5.6 7.4 CORRECT

x x

slide-18
SLIDE 18

Call Frames and Objects

  • Mutable objects can be

altered in a function call

§ Object vars hold names! § Folder accessed by both global var & parameter

  • Example:

def incr_x(q): q.x = q.x + 1 >>> p = introcs.Point3(0,0,0) >>> incr_x(p)

9/20/18 Objects 18

1 incr_x 1 id5 q

Global STUFF Call Frame

id5 p id5 0.0 …

Point3

x

slide-19
SLIDE 19

Call Frames and Objects

  • Mutable objects can be

altered in a function call

§ Object vars hold names! § Folder accessed by both global var & parameter

  • Example:

def incr_x(q): q.x = q.x + 1 >>> p = introcs.Point3(0,0,0) >>> incr_x(p)

9/20/18 Objects 19

id5 p id5 0.0 …

Point3

x 1 incr_x id5 q

Global STUFF Call Frame

1.0

x

slide-20
SLIDE 20

Call Frames and Objects

  • Mutable objects can be

altered in a function call

§ Object vars hold names! § Folder accessed by both global var & parameter

  • Example:

def incr_x(q): q.x = q.x + 1 >>> p = introcs.Point3(0,0,0) >>> incr_x(p)

9/20/18 Objects 20

id5 p id5 0.0 …

Point3

x 1

Global STUFF Call Frame

1.0

x

slide-21
SLIDE 21

Methods: Functions Tied to Objects

  • Method: function tied to object

§ Method call looks like a function call preceded by a variable name: ⟨variable⟩.⟨method⟩(⟨arguments⟩) § Example: p.distance(q) § Example: p.abs() # makes x,y,z ≥ 0

  • Just like we saw for strings

§ s = 'abracadabra' § s.index('a')

  • Are strings objects?

id3 x 5.0 y 2.0 z 3.0 id3 p

Point3

9/20/18 Objects 21

slide-22
SLIDE 22

Surprise: All Values are in Objects!

  • Including basic values

§ int, float, bool, str

  • Example:

>>> x = 2.5 >>> id(x)

  • But they are immutable

§ Contents cannot change § Distinction between value and identity is immaterial § So we can ignore the folder 2.5 x 2.5 id5 id5 x

float

9/20/18 Objects 22

slide-23
SLIDE 23

Surprise: All Values are in Objects!

  • Including basic values

§ int, float, bool, str

  • Example:

>>> x = 'foo' >>> id(x)

  • 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 'foo' id6 id6 x i n c l u d e s s t r i n g s

str

9/20/18 Objects 23

slide-24
SLIDE 24

Class Objects

  • Use name class object to

distinguish from other values

§ Not int, float, bool, str

  • Class objects are mutable

§ You can change them § Methods can have effects besides their return value

  • Example:

§ p = Point(3,-3,0) § p.clamp(-1,1)

Example: Files

f = open('jabber.txt') s = f.read() f.close() id6 f id6

name, position, state, …

Opens a file on your disk; returns a file

  • bject you can read

file

9/20/18 Objects 24

slide-25
SLIDE 25

Base Types vs. Classes

Base Types

  • Built-into Python
  • Refer to instances as values
  • Instantiate with literals
  • Are all immutable
  • Can ignore the folders

Classes

  • Provided by modules
  • Refer to instances as objects
  • Instantiate w/ constructors
  • Can alter attributes
  • Must represent with folders

9/20/18 Objects 25

slide-26
SLIDE 26

Aside: Name Resolution

  • ⟨object⟩.⟨name⟩ means

§ Go the folder for object § Look for attr/method name § If missing, check class folder

  • Class folder is a shared folder

§ Only one for the whole class § Shared by all objects of class § Stores common features § Typically where methods are

  • Do not worry about this yet

id3 x 5.0 y 2.0 z 3.0 id3 p

Point3

__init__(x, y, z) distanceTo(other) abs() Point

id4 x 7.4 y 0.0 z 0.0 id4 q

Point3

9/20/18 Objects 26

slide-27
SLIDE 27

Where To From Here?

  • Right now, just try to understand objects

§ All Python programs use objects § Most small programs use objects of classes that are part of the Python Library

  • OO Programming is about creating classes

§ Eventually you will make your own classes § Classes are the primary tool for organizing more complex Python programs § But we need to learn other basics first

9/20/18 Objects 27