http://www.cs.cornell.edu/courses/cs1110/2018sp 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]
Feb 27: CS 1110: Announcements • Last call for a one-on-one! § CMS: OPTIONAL: one-on-ones • Prelim 1 is March 13. You have until March 1st, 11:59pm to register a conflict or a need for accommodation. There is no single makeup session. See website: “Assessment à Exams” § CMS: Prelim 1 conflicts • A1 Revisions: open from Mar 1 st thru Mar 7, 11:59pm.
Storage in Python – Round 1 • Global Space § What you “start with” § Stores global variables § Lasts until you quit Python Global Space p id2
Folders Live on the Heap p = shapes.Point3(1,2,3) p lives in the Global Space. Its folder lives on the Heap. Global Space Heap Space id5 p id5 Point3 x y z 1 2 3
Storage in Python – Round 2 • 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
Calling a Function Creates a Call Frame p = shapes.Point3(1,2,3) incr_x(p) Global Space Heap Space id5 p id5 Point3 Call Frame x y z 1 2 3 incr_x 1 the_point id5
What goes in a Call Frame? p = shapes.Point3(1,2,3) Global Space p id5 def incr_x(the_point): Call Frame the_point.x = the_point.x+1 incr_x 1 incr_x(p) the_point id5 (1) Boxes for parameters at the start of the function (2) Boxes for variables local to the function as they are created
Storage in Python – Round 3 • 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
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? Write a helper function! Makes your code easier to: § Read § Write § Edit § Debug 9
From before: last_name_first def last_name_first(n): """Returns: copy of <n> but in the form <last-name>, <first-name> Precondition: <n> is in the form <first-name> <last-name> with one or more blanks between the two names. """ 1 space_index = n.find(' ') first = n[:space_index] 2 3 last = n[space_index+1:].strip() return last+', '+first 4 • last_name_first('Haruki Murakami') gives 'Murakami, Haruki' • last_name_first('Haruki Murakami') gives ' Murakami, Haruki' 10
Frames and Helper Functions def last_name_first(s): def first_name(s): """ Precondition : s in the form """ Prec : see last_name_first""" <first-name> <last-name>""" end = s.find(' ') 4 first = first_name(s) 1 return s[0:end] 5 last = last_name(s) 2 return last + ',' + first 3 def last_name(s): """ Prec : see last_name_first""" rfind gets the last instance of substring end = s.rfind(' ') 6 return s[end+1:] 7 11
Drawing Frames for Helper Functions (1) def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Haruki Murakami' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 def first_name(s): """ Prec : see last_name_first""" end = s.find(' ') 4 return s[0:end] 5 last_name_first('Haruki Murakami') 12
Drawing Frames for Helper Functions (2) Not done. Do not erase! def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Haruki Murakami' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 first_name 4 def first_name(s): s 'Haruki Murakami' """ Prec : see last_name_first""" end = s.find(' ') 4 return s[0:end] 5 last_name_first('Haruki Murakami') 13
Drawing Frames for Helper Functions (3) def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Haruki Murakami' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 first_name 5 def first_name(s): s 'Haruki Murakami' """ Prec : see last_name_first""" end = s.find(' ') end 6 4 return s[0:end] 5 last_name_first('Haruki Murakami') 14
Drawing Frames for Helper Functions (4) def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Haruki Murakami' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 first_name def first_name(s): s 'Haruki Murakami' """ Prec : see last_name_first""" end = s.find(' ') end 6 4 return s[0:end] 5 RETURN 'Haruki' last_name_first('Haruki Murakami') 15
Question: What Happens Next? A: def last_name_first(s): last_name_first 1 last_name_first 2 """ Precondition : s in the form s 'Haruki Murakami' <first-name> <last-name>""" Stuff first = first_name(s) 1 first_name ERASE FRAME #2 last = last_name(s) 2 return last + ',' + first 3 s 'Haruki Murakami' B: last_name_first 2 end 6 def first_name(s): Stuff RETURN 'Haruki' """ Prec : see last_name_first""" end = s.find(' ') 4 first_name C: ERASE FRAME #1 return s[0:end] 5 ERASE FRAME #2 Stuff last_name_first('Haruki Murakami')
Answer: What Happens Next? A: def last_name_first(s): last_name_first 1 last_name_first 2 """ Precondition : s in the form � s 'Haruki Murakami' <first-name> <last-name>""" Stuff first = first_name(s) 1 first_name ERASE FRAME #2 last = last_name(s) 2 return last + ',' + first 3 s 'Haruki Murakami' B: last_name_first 2 end 6 def first_name(s): Stuff RETURN 'Haruki' """ Prec : see last_name_first""" end = s.find(' ') 4 first_name C: ERASE FRAME #1 return s[0:end] 5 ERASE FRAME #2 Stuff last_name_first('Haruki Murakami')
Drawing Frames for Helper Functions (5) def last_name_first(s): """ Precondition : s in the form last_name_first 2 <first-name> <last-name>""" s 'Haruki Murakami' first = first_name(s) 1 last = last_name(s) 2 first 'Haruki' return last + '.' + first 3 def last_name(s): """ Prec : see last_name_first""" end = s.rfind(' ') 6 return s[end+1:] 7 last_name_first('Haruki Murakami')
Drawing Frames for Helper Functions (6) def last_name_first(s): """ Precondition : s in the form last_name_first 2 <first-name> <last-name>""" s 'Haruki Murakami' first = first_name(s) 1 last = last_name(s) 2 first 'Haruki' return last + '.' + first 3 last_name 6 def last_name(s): s 'Haruki Murakami' """ Prec : see last_name_first""" end = s.rfind(' ') 6 return s[end+1:] 7 last_name_first('Haruki Murakami') 19
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 20
Call Stack Example (1) 1 def happy_birthday(): 2 print(“Happy Birthday”) 3 def dear(): 4 print(“Dear James”) Call Frame Stack 5 def to_you(): 6 print(“to you”) 14 song 7 def line_with_name(): 8 happy_birthday() dear() 9 10 def basic_line(): happy_birthday() 11 to_you() 12 def song(): 13 basic_line() 14 basic_line() 15 line_with_name() 16 basic_line() 17 song() 18
Call Stack Example (2) 1 def happy_birthday(): 2 print(“Happy Birthday”) 3 def dear(): 4 print(“Dear James”) Call Frame Stack 5 def to_you(): 6 print(“to you”) 14 song 7 def line_with_name(): 8 happy_birthday() 11 basic_line dear() 9 10 def basic_line(): happy_birthday() 11 to_you() 12 def song(): 13 basic_line() 14 basic_line() 15 line_with_name() 16 basic_line() 17 song() 18
Call Stack Example (3) 1 def happy_birthday(): 2 print(“Happy Birthday”) 3 def dear(): 4 print(“Dear James”) Call Frame Stack 5 def to_you(): 6 print(“to you”) 14 song 7 def line_with_name(): 8 happy_birthday() 11 basic_line dear() 9 10 def basic_line(): happy_birthday() 11 2 happy_birthday to_you() 12 def song(): 13 basic_line() 14 basic_line() 15 line_with_name() 16 basic_line() 17 song() 18
Recommend
More recommend