memory in python
play

Memory in Python [Andersen, Gries, Lee, Marschner, Van Loan, White] - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 9 Memory in Python [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements: Assignment 1 A1 is graded. If your A1 is not perfect, your first grade is a 1. This is a


  1. CS 1110: Introduction to Computing Using Python Lecture 9 Memory in Python [Andersen, Gries, Lee, Marschner, Van Loan, White]

  2. Announcements: Assignment 1 • A1 is graded. If your A1 is not perfect, your first grade is a 1.  This is a counter for how many times you have submitted.  It is not a permanent grade, can resubmit. • In order to give students more chances to revise, the March 2 nd resubmit deadline is being extended until Sunday, March 5 th 11:59pm • Review the announcements from the end of Lecture 6 for policies: http://www.cs.cornell.edu/courses/cs1110/2017sp/lectures/02-14-17/presentation-06.pdf • Read Section 2.3 of A1 carefully to understand how to revise. 2/28/17 Memory in Python 2

  3. Announcements • Assignment 2 is released  Due Tuesday, March 7 th at 11:59pm  Involves writing on paper  Must turn in a legible electronic copy through CMS • Lab 5 is released (note there is no Lab 4) • Reading: Section 10.1-10.2, 10.4-10.6 • Prelim conflicts assignment on CMS due tomorrow because 1 st Prelim is March 14th 2/28/17 Memory in Python 3

  4. Storage in Python • Global Space Global Space  What you “start with” p id2  Stores global variables Call Frame  Also modules & functions!  Lasts until you quit Python incr_x • Call Frame id2 q  Variables in function call  Deleted when call done Heap Space id2 • Heap Space Point3  Where “folders” are stored x y x 1.0 2.0 3.0  Have to access indirectly 2/28/17 Memory in Python 4

  5. Memory and the Python Tutor Heap Space Global Space Call Frame 2/28/17 Memory in Python 5

  6. Functions and Global Space • A function definition… def to_celsius(x): Body  Creates a global variable return 5*(x-32)/9.0 (same name as function) Global Space  Creates a folder for body  Puts folder id in variable id6 to_celsius • OPT Link: https://goo.gl/iBfxyo Heap Space id6 function Body 2/28/17 Memory in Python 6

  7. Modules and Global Space import math • import …  Creates a global variable Global Space (same name as module) math id5 Heap Space  Puts contents in a folder id5 • variables, functions module  Puts folder id in variable 3.141592 pi • from dumps contents to 2.718281 e global space functions • OPT: https://goo.gl/4LYvwl 2/28/17 Memory in Python 7

  8. Modules vs Objects Module Object id2 id3 math p id2 id3 module Point3 5.0 x 3.141592 pi 2.0 y 2.718281 e 3.0 z math.pi p.x functions math.cos(1) p.distanceTo(q) 2/28/17 Memory in Python 8

  9. Frames and Helper Functions • Functions can call each other! • Each call creates a new call frame • Function that exists mainly to call other functions is often called a helper function 2/28/17 Memory in Python 9

  10. 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. No leading or trailing spaces.""" 1 2 space_index = n.find(' ') 3 first = n[:space_index] 4 last = n[space_index+1:].strip() return last+', '+first • last_name_first('Erik Andersen') gives 'Andersen, Erik' • last_name_first('Erik Andersen') gives ' Andersen, Erik' 2/28/17 Memory in Python 10

  11. Frames and Helper Functions def first_name(s): """ Prec : see last_name_first""" end = s.find(' ') 4 return s[0:end] 5 def last_name(s): """ Prec : see last_name_first""" 6 end = s.rfind(' ') rfind gets the last instance of substring 7 return s[end+1:] 2/28/17 Memory in Python 11

  12. Frames and Helper Functions def last_name_first(s): def first_name(s): """ Precondition : s in the form """ Prec : 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""" 6 end = s.rfind(' ') 7 return s[end+1:] 2/28/17 Memory in Python 12

  13. Frames and Helper Functions Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Erik Andersen' 1 first = first_name(s) 2 last = last_name(s) 3 return last + ',' + first def first_name(s): """ Prec : see last_name_first""" end = s.find(' ') 4 5 return s[0:end] 2/28/17 Memory in Python 13

  14. Frames and Helper Functions Not done. Do not erase! Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Erik Andersen' 1 first = first_name(s) 2 last = last_name(s) 3 return last + ',' + first first_name 4 def first_name(s): s 'Erik Andersen' """ Prec : see last_name_first""" end = s.find(' ') 4 5 return s[0:end] 2/28/17 Memory in Python 14

  15. Frames and Helper Functions Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Erik Andersen' 1 first = first_name(s) 2 last = last_name(s) 3 return last + ',' + first first_name 5 def first_name(s): s 'Erik Andersen' """ Prec : see last_name_first""" end = s.find(' ') 4 end 4 5 return s[0:end] 2/28/17 Memory in Python 15

  16. Frames and Helper Functions Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Erik Andersen' 1 first = first_name(s) 2 last = last_name(s) 3 return last + ',' + first first_name def first_name(s): s 'Erik Andersen' """ Prec : see last_name_first""" end = s.find(' ') 4 end 4 5 return s[0:end] RETURN 'Erik' 2/28/17 Memory in Python 16

  17. What happens next? Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" 1 first = first_name(s) s 'Erik Andersen' B: last_name_first 2 2 last = last_name(s) return last + ',' + first 3 first_name Stuff A: last_name_first 2 s 'Erik Andersen' first_name Stuff end 4 Stuff RETURN 'Erik' ERASE FRAME #2 C: ERASE FRAME #1 ERASE FRAME #2 2/28/17 Memory in Python 17

  18. Frames and Helper Functions Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 2 <first-name> <last-name>""" s 'Erik Andersen' 1 first = first_name(s) 2 last = last_name(s) first 'Erik' 3 return last + ',' + first def first_name(s): """ Prec : see last_name_first""" end = s.find(' ') 4 5 return s[0:end] 2/28/17 Memory in Python 18

  19. Frames and Helper Functions Call: last_name_first('Erik Andersen'): def last_name_first(s): """ Precondition : s in the form last_name_first 2 <first-name> <last-name>""" s 'Erik Andersen' 1 first = first_name(s) 2 last = last_name(s) first 'Erik' 3 return last + '.' + first last_name 6 def last_name(s): s 'Erik Andersen' """ Prec : see last_name_first""" end = s.rfind(' ') 6 7 return s[end+1:] 2/28/17 Memory in Python 19

  20. The Call Stack • Functions are “stacked”  Cannot remove one above Frame 1 calls w/o removing one below  Sometimes draw bottom up Frame 2 calls (better fits the metaphor) Frame 3 • Python must keep the entire calls stack in memory Frame 4 calls  Error if it cannot hold stack Frame 6 Frame 5 2/28/17 Memory in Python 20

  21. The Call Stack • Functions are “stacked”  Cannot remove one above Frame 1 calls w/o removing one below  Sometimes draw bottom up Frame 2 calls (better fits the metaphor) Frame 3 • Python must keep the entire calls stack in memory Frame 4  Error if it cannot hold stack 2/28/17 Memory in Python 21

  22. The Call Stack • Functions are “stacked”  Cannot remove one above Frame 1 calls w/o removing one below  Sometimes draw bottom up Frame 2 calls (better fits the metaphor) Frame 3 • Python must keep the entire calls stack in memory Frame 4 calls  Error if it cannot hold stack Frame 6 2/28/17 Memory in Python 22

  23. Example OPT Link: https://goo.gl/ckBJh9 def function_1(x,y): return function_2(x,y) calls def function_2(x,y): return function_3(x,y) calls def function_3(x,y): return x+y calls print function_1(1,0) 2/28/17 Memory in Python 23

  24. Errors and the Call Stack # error.py def function_1(x,y): return function_2(x,y) def function_2(x,y): return function_3(x,y) def function_3(x,y): Crashes here return x/y (division by 0) print function_1(1,0) 2/28/17 Memory in Python 24

Recommend


More recommend