http://www.cs.cornell.edu/courses/cs1110/2019sp 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]
Global Space • Global Space Global Space § What you “start with” x 4 § Stores global variables § Lasts until you quit Python x = 4
Enter Heap Space • Global Space Global Space Heap Space § What you “start with” id1 x 4 § Stores global variables Point2 § Lasts until you quit Python p id1 1 x • Heap Space q id2 2 y § Where “folders” are stored § Have to access indirectly id2 Point2 x = 4 10 x p = shape.Point2(1,2) q = shape.Point2(10,7) 7 y p & q live in Global Space. Their folders live on the Heap.
Calling a Function Creates a Call Frame What’s in a Call Frame? Global Space Heap Space • Boxes for parameters at id1 the start of the function x 4 Point2 • Boxes for variables local to p id1 1 x the function as they are 2 y created Call Frame def adjust_x_coord(pt, n): adjust_x_coord 1 pt.x = pt.x + n 1 pt id1 x = 4 n 4 p = shape.Point2(1,2) adjust_x_coord(p, x)
Calling a Function Creates a Call Frame What’s in a Call Frame? Global Space Heap Space • Boxes for parameters at id1 the start of the function x 4 Point2 • Boxes for variables local to p id1 1 5 x the function as they are 2 y created Call Frame def adjust_x_coord(pt, n): adjust_x_coord 1 pt.x = pt.x + n 1 pt id1 x = 4 n 4 p = shape.Point2(1,2) adjust_x_coord(p, x) RETURN None
Putting it all together • Global Space § What you “start with” § Stores global variables § Lasts until you quit Python Global Space Heap Space p id2 • Heap Space id2 § Where “folders” are stored § Have to access indirectly • Call Frames f1 Call Frames § Parameters f2 § Other variables local to function § Lasts until function returns
2 Points Make a Line! 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) Where does the line start? print(“The line starts at (”+x+ “,”+y+ “).” ) x: 1 y: 2 print(“Where does the line stop?”) The line starts at (1,2). x = input(“x: ”) Where does the line stop? stop.x = int(x) x: 4 y = input(“y: ”) y: 6 stop.y = int(y) The line stops at (4,6). print(“The line stops at (”+x+ “,”+y+ “).” ) 7
Redundant Code is BAAAAD! 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+ “).” ) 8
Let’s make a function! 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”) 9
Still a bit of redundancy 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”) 10
Yay, Helper Functions! def get_coord(name): x = input(name+“: ”) return str(x) ß Actual bug I wrote in Only have to fix 1 line. int my code. Not staged! In the first version, I def configure(pt, role): would have had to fix print(“Where does the line ” + role + “?”) it in 4 places! 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”) 11
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 12 § Debug
Drawing Frames for Helper Functions (1) Call Frames def get_coord(name): x = input(name+“: ”) configure 1 3 4 return int(x) 2 pt id1 role “start” def configure(pt, role): print(“Where does the line ” + role + “?”) 3 pt.x = get_coord(“x”) 4 pt.y = get_coord(“y”) 5 print(“The line ” +role+ “s at (”+str(pt.x)+ 6 “,”+str(pt.y)+ “).” ) start = shape.Point2(0,0) configure(start, “start”) 13
Q: what do you do next? Call Frames def get_coord(name): x = input(name+“: ”) configure 1 3 4 return int(x) 2 pt id1 role “start” def configure(pt, role): print(“Where does the line ” + role + “?”) 3 pt.x = get_coord(“x”) 4 A: Cross out the configure call frame. pt.y = get_coord(“y”) 5 B: Create a get_coord call frame. print(“The line ” +role+ “s at (”+str(pt.x)+ 6 C: Cross out the 4 in the call frame. “,”+str(pt.y)+ “).” ) D: A & B E: B & C start = shape.Point2(0,0) configure(start, “start”) 14
Drawing Frames for Helper Functions (2) Call Frames def get_coord(name): x = input(name+“: ”) Not done! configure 1 3 4 Do not cross return int(x) 2 pt id1 out!! role “start” def configure(pt, role): print(“Where does the line ” + role + “?”) 3 get_coord pt.x = get_coord(“x”) 1 4 pt.y = get_coord(“y”) 5 name “x” print(“The line ” +role+ “s at (”+str(pt.x)+ 6 “,”+str(pt.y)+ “).” ) start = shape.Point2(0,0) configure(start, “start”) 15
Drawing Frames for Helper Functions (3) Call Frames def get_coord(name): x = input(name+“: ”) configure 1 3 4 return int(x) 2 pt id1 role “start” def configure(pt, role): print(“Where does the line ” + role + “?”) 3 get_coord pt.x = get_coord(“x”) 1 2 4 pt.y = get_coord(“y”) 5 name “x” print(“The line ” +role+ “s at (”+str(pt.x)+ 6 x “1” “,”+str(pt.y)+ “).” ) RETURN 1 start = shape.Point2(0,0) configure(start, “start”) 16
Drawing Frames for Helper Functions (4) Call Frames def get_coord(name): x = input(name+“: ”) configure 1 3 4 5 return int(x) 2 pt id1 role “start” def configure(pt, role): print(“Where does the line ” + role + “?”) 3 get_coord pt.x = get_coord(“x”) 1 2 4 pt.y = get_coord(“y”) 5 name “x” print(“The line ” +role+ “s at (”+str(pt.x)+ 6 x “1” “,”+str(pt.y)+ “).” ) RETURN 1 start = shape.Point2(0,0) configure(start, “start”) 17
The Call Stack • Functions frames are “stacked” § Cannot remove one above function1 calls w/o removing one below function2 • Python must keep the entire calls stack in memory function3 § Error if it cannot hold stack calls (“stack overflow”) function4 calls function5 18
Q: what does the call stack look like at this point in the execution of the code? def f3(): A B C D E print(“f3”) f1 f1 f1 f1 f1 def f2(): f2 f2 f2 f2 print(“f2”) f3 f3 f3 f3() f3() f3 f3 f3() f3 def f1(): print(“f1”) f2() 19 f1()
Q: what does the call stack look like at this point in the execution of the code? def f3(): A B C D E print(“f3”) f1 f1 f1 f1 f1 def f2(): f2 f2 f2 f2 print(“f2”) f3 f3 f3 f3() f3() f3 f3 f3() f3 def f1(): print(“f1”) f2() 20 f1()
Errors and the Call Stack def get_coord(name): x = input(name+“: ”) Where does the line start? 1 x: 1 return int( x1 ) 2 Traceback (most recent call last): File "v3.py", line 15, in <module> configure(start, "start") def configure(pt, role): File "v3.py", line 9, in configure pt.x = get_coord("x") print(“Where does the line ” + role + “?”) 3 File "v3.py", line 5, in get_coord pt.x = get_coord(“x”) return str( x1 ) 4 NameError: name ' x1 ' is not defined pt.y = get_coord(“y”) 5 print(“The line ” +role+ “s at (”+x+ “,”+y+ “).” ) 6 start = shape.Point2(0,0) configure(start, “start”) 21
Modules and Global Space import • Creates a global variable (same name as module) import math • Puts variables, functions in a folder • Puts folder id in variable Global Space Heap Space id5 math id5 module 3.141592 e 2.718281 pi functions 22
Modules vs Objects Heap Space >>> import math >>> math.pi id5 math 3.141592 e 2.718281 pi >>> p = shapes.Point3(5,2,3) >>> p.x functions Global Space id3 math Point3 id5 5 x 3 2 z y p id3 23
Storage in Python • Global Space § What you “start with” § Stores global variables, modules & functions § Lasts until you quit Python Global Space Heap Space p id2 • Heap Space id2 § Where “folders” are stored § Have to access indirectly Call Frame Stack • Call Frame Stack f1 § Parameters f2 § Other variables local to function § Lasts until function returns
Recommend
More recommend