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

objects
SMART_READER_LITE
LIVE PREVIEW

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:


slide-1
SLIDE 1

Objects

Lecture 7

CS 1110:

Introduction to Computing Using Python

[Andersen, Gries, Lee, Marschner, Van Loan, White]

slide-2
SLIDE 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

slide-3
SLIDE 3

Review: Types

  • 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)

2/16/17 Objects 3

slide-4
SLIDE 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
  • This can get really messy
  • How about a single variable

that represents a point?

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

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
  • This can get really messy
  • How about a single variable

that represents a point?

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

2/16/17 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
  • Object type is a class
  • Modules provide classes
  • Will show how later
  • Example: geom
  • Classes: Point2, Point3

2/16/17 Objects 7

id1 x 2.0 y 3.0 z 5.0

Point3

class name

slide-8
SLIDE 8

Classes: Types for Objects

Classes

  • Point3
  • Point2

Types

  • int
  • float
  • bool
  • str
  • Classes are how we add new types to Python
  • Sort of like a template

Class

2/16/17 Objects 8

Point3s have an x, y, and z Point3

slide-9
SLIDE 9

Constructor: Function to make Objects

  • How do we create objects?
  • Constructor Function:
  • Format: ⟨class name⟩(⟨arguments⟩)
  • Example: Point3(0.0,0.0,0.0)
  • Makes a new object (manila folder)

with a new id

  • Called an instantiated object
  • Returns folder id as value

2/16/17 Objects 9

instantiated

  • bject

id2 x 0.0 y 0.0 z 0.0

Point3

Point3s have an x, y, and z Point3 new id (in this case id2)

slide-10
SLIDE 10

Constructor: Function to make Objects

  • How do we create objects?
  • Constructor Function:
  • Format: ⟨class name⟩(⟨arguments⟩)
  • Example: Point3(0.0,0.0,0.0)
  • Makes a new object (manila folder)

with a new id

  • Called an instantiated object
  • Returns folder id as value
  • Example: p = Point3(0.0, 0.0, 0.0)
  • Creates a Point object
  • Stores object’s id in p

2/16/17 Objects 10

id2 p Variable stores ID not object instantiated

  • bject

id2 x 0.0 y 0.0 z 0.0

Point3

Like a Greek god!

slide-11
SLIDE 11

Constructors and Modules

>>> import geom >>> p = geom.Point3(0.0,0.0,0.0) >>> id(p)

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

slide-12
SLIDE 12

Accessing Attributes

  • Attributes are variables

that live inside of objects

  • Can use in expressions
  • Can assign values to them
  • Format: ⟨variable⟩.⟨attribute⟩
  • Example: p.x
  • Look like module variables
  • To evaluate p.x, Python:
  • 1. finds folder with id stored in p
  • 2. returns the value of x in that folder

2/16/17 Objects 12

id3 x 1.0 y 2.0 z 3.0 id3 p

Point3

slide-13
SLIDE 13

Accessing Attributes

  • Example:
  • p = geom.Point3(1.0, 2.0, 3.0)
  • p.x = p.y + p.z

2/16/17 Objects 13

id3 x 1.0 y 2.0 z 3.0 id3 p

Point3

5.0

x

slide-14
SLIDE 14

Object Variables

  • Variable stores object id
  • Reference to the object
  • Reason for folder analogy
  • Assignment uses object id
  • Example: liked = disliked
  • Takes contents from disliked
  • Puts contents in liked
  • Does not make new folder!
  • This is the cause of many

mistakes in this course

2/16/17 Objects 14

id2 disliked id2 x 0.0 y 0.0 z 0.0

Point3

id2 liked

slide-15
SLIDE 15

Exercise: Attribute Assignment

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

  • Execute the assignments:

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

  • What is value of p.x?

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

CORRECT

slide-16
SLIDE 16

Exercise: Attribute Assignment

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

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

  • Execute the assignments:

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

  • What is value of p.x?
slide-17
SLIDE 17

Exercise: Attribute Assignment

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

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

  • Execute the assignments:

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

  • What is value of p.x?
slide-18
SLIDE 18

>>> p = 5.0 >>> q = p >>> p = 4.0 >>> q 5.0 >>> from geom import * >>> p = Point3(1.0,2.0,3.0) >>> q = p >>> p.x = 4.0 >>> q.x 4.0

The rules of variables have not changed! However, combining variable assignment with object references can be confusing.

Assignment and Attribute Oddness

2/16/17 Objects 18

!!

slide-19
SLIDE 19

Call Frames and Objects

  • Objects can be altered in a

function call

  • Object variables hold ids!
  • Folder can be accessed from

global variable or parameter

  • Example:

def incr_x(q): q.x = q.x + 1.0

>>> p = geom.Point3(1.0, 2.0, 3.0) >>> incr_x(p)

2/16/17 Objects 19

1 incr_x 1 id5 q

Global STUFF Call Frame

id5 p id5 1.0 …

Point3

x

slide-20
SLIDE 20

Call Frames and Objects

  • Objects can be altered in a

function call

  • Object variables hold ids!
  • Folder can be accessed from

global variable or parameter

  • Example:

def incr_x(q): q.x = q.x + 1.0

>>> p = geom.Point3(1.0, 2.0, 3.0) >>> incr_x(p)

2/16/17 Objects 20

1 incr_x id5 q

Global STUFF Call Frame

id5 p id5 1.0 2.0 …

Point3

x

x

slide-21
SLIDE 21

Call Frames and Objects

  • Objects can be altered in a

function call

  • Object variables hold ids!
  • Folder can be accessed from

global variable or parameter

  • Example:

def incr_x(q): q.x = q.x + 1.0

>>> p = geom.Point3(1.0, 2.0, 3.0) >>> incr_x(p)

2/16/17 Objects 21

1

Global STUFF Call Frame

id5 p id5 1.0 2.0 …

Point3

x

x

slide-22
SLIDE 22

Exercise: Attribute Assignment

2/16/17 Objects 22

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) Draw everything that gets created. How many folders get drawn?

2/16/17

slide-23
SLIDE 23

Exercise: Attribute Assignment

2/16/17 Objects 23

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) Draw everything that gets created. How many folders get drawn?

2/16/17

Point3 x 1.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2

slide-24
SLIDE 24

Exercise: Attribute Assignment

2/16/17 Objects 24

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) Draw everything that gets created. How many folders get drawn? What else gets drawn?

2/16/17

Point3 x 1.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2

slide-25
SLIDE 25

Exercise: Attribute Assignment

2/16/17 Objects 25

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) Draw everything that gets created. How many folders get drawn? What else gets drawn?

2/16/17

Point3 x 1.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2 id1 p id2 q

slide-26
SLIDE 26

Exercise: Attribute Assignment

2/16/17 Objects 26

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t 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

slide-27
SLIDE 27

Exercise: Attribute Assignment

2/16/17 Objects 27

def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t id1 p id2 q p q t swap_x:1 Point3 x 1.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2 import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) p q swap_x

slide-28
SLIDE 28

Exercise: Attribute Assignment

2/16/17 Objects 28

def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t id1 p id2 q p q t swap_x:1 Point3 x 1.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2 import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) p q id1 id2 swap_x 1

slide-29
SLIDE 29

Exercise: Attribute Assignment

2/16/17 Objects 29

def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t id1 p id2 q p q t swap_x:1 Point3 x 1.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2 import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) p q t id1 id2 1.0 swap_x 2

slide-30
SLIDE 30

Exercise: Attribute Assignment

2/16/17 Objects 30

def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t id1 p id2 q p q t swap_x:1 Point3 x 1.0 3.0 y 2.0 z 3.0 id1 Point3 x 3.0 y 4.0 z 5.0 id2 import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) p q t id1 id2 1.0 swap_x 3

x

slide-31
SLIDE 31

Exercise: Attribute Assignment

2/16/17 Objects 31

def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t id1 p id2 q p q t swap_x:1 Point3 x 1.0 3.0 y 2.0 z 3.0 id1 Point3 x 3.0 1.0 y 4.0 z 5.0 id2 import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) p q t id1 id2 1.0 swap_x

x x

slide-32
SLIDE 32

Exercise: Attribute Assignment

2/16/17 Objects 32

def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t id1 p id2 q Point3 x 1.0 3.0 y 2.0 z 3.0 id1 Point3 x 3.0 1.0 y 4.0 z 5.0 id2 import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q)

x x

slide-33
SLIDE 33

Exercise: Attribute Assignment

2/16/17 Objects 33

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap_x(p, q) def swap_x(p, q):

1

t = p.x

2

p.x = q.x

3

q.x = t 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 CORRECT

slide-34
SLIDE 34

Exercise: Attribute Assignment

2/16/17 Objects 34

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t What is in global p after calling swap? A: id1 B: id2 C: I don’t know id1 p id2 q Before calling swap(p, q):

slide-35
SLIDE 35

Exercise: Attribute Assignment

2/16/17 Objects 35

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t id1 p id2 q p q id1 id2 Point x 1.0 y 2.0 z 3.0 id1 Point x 3.0 y 4.0 z 5.0 id2 swap 1

slide-36
SLIDE 36

Exercise: Attribute Assignment

2/16/17 Objects 36

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t id1 p id2 q p q t id1 id2 id1 Point x 1.0 y 2.0 z 3.0 id1 Point x 3.0 y 4.0 z 5.0 id2 swap 2

slide-37
SLIDE 37

Exercise: Attribute Assignment

2/16/17 Objects 37

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t id1 p id2 q p q t swap id1 id2 id2 id1 Point x 1.0 y 2.0 z 3.0 id1 Point x 3.0 y 4.0 z 5.0 id2

x

3

slide-38
SLIDE 38

Exercise: Attribute Assignment

2/16/17 Objects 38

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t id1 p id2 q p q t id1 id2 id2 id1 id1 Point x 1.0 y 2.0 z 3.0 id1 Point x 3.0 y 4.0 z 5.0 id2

x x

swap

slide-39
SLIDE 39

Exercise: Attribute Assignment

2/16/17 Objects 39

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t id1 p id2 q Point x 1.0 y 2.0 z 3.0 id1 Point x 3.0 y 4.0 z 5.0 id2

slide-40
SLIDE 40

Exercise: Attribute Assignment

2/16/17 Objects 40

import geom p = geom.Point3(1.0,2.0,3.0) q = geom.Point3(3.0,4.0,5.0) swap(p, q) def swap(p, q):

1

t = p

2

p = q

3

q = t What is in global p after calling swap? A: id1 B: id2 C: I don’t know CORRECT id1 p id2 q