lecture 9 memory in python
play

Lecture 9: Memory in Python CS 1110 Introduction to Computing - PowerPoint PPT Presentation

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


  1. 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]

  2. 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.

  3. Storage in Python – Round 1 • Global Space § What you “start with” § Stores global variables § Lasts until you quit Python Global Space p id2

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  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. """ 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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')

  17. 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')

  18. 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')

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend