lecture 9: memory in python - cornell university · p = shape.point2(1,2) adjust_x_coord(p, x) pid1...

24
Lecture 9: Memory in Python CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] http://www.cs.cornell.edu/courses/cs1110/2019sp

Upload: others

Post on 21-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Lecture 9: Memory in Python

CS 1110Introduction to Computing Using Python

[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

http://www.cs.cornell.edu/courses/cs1110/2019sp

Page 2: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Global Space• Global Space

§ What you “start with”§ Stores global variables§ Lasts until you quit Python

x = 4

4x

Global Space

Page 3: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Enter Heap Space• Global Space

§ What you “start with”§ Stores global variables§ Lasts until you quit Python

• Heap Space§ Where “folders” are stored§ Have to access indirectly

4x

Global Space Heap Space

x = 4p = shape.Point2(1,2)q = shape.Point2(10,7)

id1pid1

Point21x2y

id2Point2

10x7y

id2q

p & q live in Global Space. Their folders live on the Heap.

Page 4: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Calling a Function Creates a Call Frame

4x

Global Space Heap Space

def adjust_x_coord(pt, n):pt.x = pt.x + n

x = 4p = shape.Point2(1,2)adjust_x_coord(p, x)

id1pid1

Point21x2y

1

Call Frameadjust_x_coord 1

id1pt4n

What’s in a Call Frame?• Boxes for parameters at

the start of the function• Boxes for variables local to

the function as they are created

Page 5: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Calling a Function Creates a Call Frame

4x

Global Space Heap Space

def adjust_x_coord(pt, n):pt.x = pt.x + n

x = 4p = shape.Point2(1,2)adjust_x_coord(p, x)

id1pid1

Point21 5x2y

1

Call Frameadjust_x_coord 1

id1pt4n

What’s in a Call Frame?• Boxes for parameters at

the start of the function• Boxes for variables local to

the function as they are created

NoneRETURN

Page 6: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Putting it all together• Global Space

§ What you “start with”§ Stores global variables§ Lasts until you quit Python

• Heap Space§ Where “folders” are stored§ Have to access indirectly

• Call Frames§ Parameters§ Other variables local to function§ Lasts until function returns

id2pGlobal Space

id2Heap Space

f1

f2Ca

ll Fr

ames

Page 7: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

2 Points Make a Line!

7

start = shape.Point2(0,0)stop = shape.Point2(0,0)print(“Where does the line start?”)x = input(“x: ”)start.x = int(x)y = input(“y: ”)start.y = int(y)print(“The line starts at (”+x+ “,”+y+ “).” ) print(“Where does the line stop?”)x = input(“x: ”)stop.x = int(x)y = input(“y: ”)stop.y = int(y)print(“The line stops at (”+x+ “,”+y+ “).” )

Where does the line start?x: 1y: 2The line starts at (1,2).Where does the line stop?x: 4y: 6The line stops at (4,6).

Page 8: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Redundant Code is BAAAAD!

8

start = shape.Point2(0,0)stop = shape.Point2(0,0)print(“Where does the line start?”)x = input(“x: ”)start.x = int(x)y = input(“y: ”)start.y = int(y)print(“The line starts at (”+x+ “,”+y+ “).” ) print(“Where does the line stop?”)x = input(“x: ”)stop.x = int(x)y = input(“y: ”)stop.y = int(y)print(“The line stops at (”+x+ “,”+y+ “).” )

Page 9: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Let’s make a function!

9

def configure(pt, role): print(“Where does the line ” + role + “?”)x = input(“x: ”)pt.x = int(x)y = input(“y: ”)pt.y = int(y)print(“The line ” +role+ “s at (”+x+ “,”+y+ “).” )

start = shape.Point2(0,0)stop = shape.Point2(0,0)configure(start, “start”)configure(stop, “stop”)

Page 10: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Still a bit of redundancy

10

def configure(pt, role): print(“Where does the line ” + role + “?”)x = input(“x: ”)pt.x = int(x)y = input(“y: ”)pt.y = int(y)print(“The line ” +role+ “s at (”+x+ “,”+y+ “).” )

start = shape.Point2(0,0)stop = shape.Point2(0,0)configure(start, “start”)configure(stop, “stop”)

Page 11: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Yay, Helper Functions!

11

def get_coord(name):x = input(name+“: ”)return str(x)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+x+ “,”+y+ “).” )

start = shape.Point2(0,0)stop = shape.Point2(0,0)configure(start, “start”)configure(stop, “stop”)

ßActual bug I wrote in my code. Not staged!

Only have to fix 1 line.In the first version, I would have had to fix it in 4 places!

int

Page 12: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Frames and Helper Functions• Functions can call each other!• Each call creates a new call frame• Writing the same several lines of code in 2

places? Or code that accomplishes some conceptual sub-task? Or your function is getting too long? Write a helper function! Makes your code easier to:§ Read§ Write§ Edit§ Debug

12

Page 13: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Drawing Frames for Helper Functions (1)

def get_coord(name):x = input(name+“: ”)return int(x)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+str(pt.x)+

“,”+str(pt.y)+ “).” ) start = shape.Point2(0,0)configure(start, “start”) 13

12

3

45

configure 3 4

id1pt

6

Call Frames

“start”role

Page 14: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Q: what do you do next?

def get_coord(name):x = input(name+“: ”)return int(x)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+str(pt.x)+

“,”+str(pt.y)+ “).” ) start = shape.Point2(0,0)configure(start, “start”) 14

12

3

45

configure 3 4

id1pt

6

Call Frames

“start”role

A: Cross out the configure call frame.B: Create a get_coord call frame.C: Cross out the 4 in the call frame.D: A & B E: B & C

Page 15: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

def get_coord(name):x = input(name+“: ”)return int(x)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+str(pt.x)+

“,”+str(pt.y)+ “).” ) start = shape.Point2(0,0)configure(start, “start”) 15

get_coord

“x”name

1

configure

id1pt

Call Frames

“start”role

12

3

456

Not done! Do not cross

out!!

Drawing Frames for Helper Functions (2)

3 4

Page 16: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

def get_coord(name):x = input(name+“: ”)return int(x)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+str(pt.x)+

“,”+str(pt.y)+ “).” ) start = shape.Point2(0,0)configure(start, “start”) 16

get_coord

“x”name

1 2

Drawing Frames for Helper Functions (3)

configure

id1pt

Call Frames

“start”role

12

3

456 “1”x

1RETURN

3 4

Page 17: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

def get_coord(name):x = input(name+“: ”)return int(x)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+str(pt.x)+

“,”+str(pt.y)+ “).” ) start = shape.Point2(0,0)configure(start, “start”) 17

get_coord

“x”name

1 2

Drawing Frames for Helper Functions (4)

configure 3 4 5

id1pt

Call Frames

“start”role

12

3

456 x

1RETURN

“1”

Page 18: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

The Call Stack• Functions frames are “stacked”

§ Cannot remove one above w/o removing one below

• Python must keep the entire stack in memory§ Error if it cannot hold stack

(“stack overflow”)

18

function1calls

calls

calls

calls

function2

function3

function4

function5

Page 19: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Q: what does the call stack look like at this point in the execution of the code?

19

def f3():print(“f3”)

def f2():print(“f2”)f3()f3()f3()

def f1():print(“f1”)f2()

f1()

f1

f2

f3

A

f3

f3

f1

f2

f3

B

f3

f1

f2

f3

Cf1

f2

Df1

E

Page 20: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Q: what does the call stack look like at this point in the execution of the code?

20

def f3():print(“f3”)

def f2():print(“f2”)f3()f3()f3()

def f1():print(“f1”)f2()

f1()

f1

f2

f3

A

f3

f3

f1

f2

f3

B

f3

f1

f2

f3

Cf1

f2

Df1

E

Page 21: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

21

Errors and the Call Stack

def get_coord(name):x = input(name+“: ”)return int(x1)

def configure(pt, role): print(“Where does the line ” + role + “?”)pt.x = get_coord(“x”)pt.y = get_coord(“y”) print(“The line ” +role+ “s at (”+x+ “,”+y+ “).” )

start = shape.Point2(0,0)configure(start, “start”)

12

3

456

Where does the line start?x: 1Traceback (most recent call last):

File "v3.py", line 15, in <module>configure(start, "start")

File "v3.py", line 9, in configurept.x = get_coord("x")

File "v3.py", line 5, in get_coordreturn str(x1)

NameError: name 'x1' is not defined

Page 22: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Modules and Global Space

import math

22

id5math id5module

import• Creates a global variable (same name as module)

• Puts variables, functions in a folder

• Puts folder id in variable

pi 3.141592 e 2.718281

functions

Heap Space Global Space

Page 23: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Modules vs Objects

23id3p

id3

x 5

y 2 z 3

Point3

>>> import math>>> math.pi

>>> p = shapes.Point3(5,2,3)>>> p.x

Global Space

id5math

Heap Spaceid5

math

pi 3.141592 e 2.718281

functions

Page 24: Lecture 9: Memory in Python - Cornell University · p = shape.Point2(1,2) adjust_x_coord(p, x) pid1 id1 Point2 x 1 y 2 1 Call Frame adjust_x_coord 1 ptid1 n 4 What’s in a Call Frame?

Storage in Python

id2p id2

f1

f2

• Global Space§ What you “start with”§ Stores global variables, modules & functions§ Lasts until you quit Python

• Heap Space§ Where “folders” are stored§ Have to access indirectly

• Call Frame Stack§ Parameters§ Other variables local to function§ Lasts until function returns

Heap Space Global Space

Call

Fram

e St

ack