memory in python announcements for this lecture
play

Memory in Python Announcements For This Lecture Assignment 1 More - PowerPoint PPT Presentation

Lecture 10 Memory in Python Announcements For This Lecture Assignment 1 More Assignments Assignment 2 TONIGHT Work on your revisions Scan and submit online Read feedback carefully Upload before midnight Want done by tomorrow


  1. Lecture 10 Memory in Python

  2. Announcements For This Lecture Assignment 1 More Assignments • Assignment 2 TONIGHT • Work on your revisions § Scan and submit online § Read feedback carefully § Upload before midnight § Want done by tomorrow § Late: -10% per day § Partial credit after Wed. § No lates after Thursday • Survey : 660 responded • Assignment 3 is posted § Deadline is tomorrow § Due week from Friday § Avg Time : 6.8 hours! § Before you go on Fall Break § STD Dev : 3.9 hours § Graded when you get back 9/25/18 Memory in Python 2

  3. Modeling 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 q id2 § 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 9/25/18 Memory in Python 3

  4. Memory and the Python Tutor Heap Space Global Space Call Frame 9/25/18 Memory in Python 4

  5. Functions and Global Space • A function definition… def to_centigrade(x): Body § Creates a global variable return 5*(x-32)/9.0 (same name as function) Global Space § Creates a folder for body to_centigrade id6 § Puts folder id in variable • Variable vs. Call Heap Space >>> to_centigrade id6 <fun to_centigrade at 0x100498de8> function >>> to_centigrade (32) Body 0.0 9/25/18 Memory in Python 5

  6. Modules and Global Space import math • Importing a module: § Creates a global variable Global Space (same name as module) math id5 Heap Space § Puts contents in a folder id5 • Module variables module • Module functions 3.141592 pi § Puts folder id in variable 2.718281 e • from keyword dumps functions contents to global space 9/25/18 Memory in Python 6

  7. Modules vs Objects Module Object math id2 p id3 id2 id3 module Point3 5.0 x 3.141592 pi 2.0 y 2.718281 e 3.0 z functions 9/25/18 Memory in Python 7

  8. Modules vs Objects Module Object math id2 p id3 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.clamp(-1,1) 9/25/18 Memory in Python 8

  9. Modules vs Objects Module Object math id2 p id3 id2 id3 The period (.) means module Point3 “go inside of the folder” 5.0 x 3.141592 pi 2.0 y 2.718281 e 3.0 z math.pi p.x functions math.cos(1) p.clamp(-1,1) 9/25/18 Memory in Python 9

  10. Recall: Call Frames Call : to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to_centigrade 1 to the parameter (in frame) 3. Execute the function body x 50.0 § Look for variables in the frame § If not there, look for global variables with that name 4. Erase the frame for the call What is happening here? def to_centigrade(x): return 5*(x-32)/9.0 Only at the End! 1 9/25/18 Memory in Python 10

  11. Recall: Call Frames Call : to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to_centigrade to the parameter (in frame) 3. Execute the function body x 50.0 § Look for variables in the frame § If not there, look for global RETURN 10.0 variables with that name 4. Erase the frame for the call def to_centigrade(x): return 5*(x-32)/9.0 1 9/25/18 Memory in Python 11

  12. Recall: Call Frames Call : to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value ERASE WHOLE FRAME to the parameter (in frame) 3. Execute the function body § Look for variables in the frame § If not there, look for global variables with that name 4. Erase the frame for the call def to_centigrade(x): But don’t actually return 5*(x-32)/9.0 1 erase on an exam 9/25/18 Memory in Python 12

  13. Aside: What Happens Each Frame Step? • The instruction counter always changes • The contents only change if § You add a new variable § You change an existing variable § You delete a variable • If a variable refers to a mutable object § The contents of the folder might change 9/25/18 Memory in Python 13

  14. Function Access to Global Space • All function definitions Global Space a 4 (for globals.py) are in some module • Call can access global show_a 1 space for that module § math.cos : global for math § temperature.to_centigrade # globals.py uses global for temperature """Show how globals work""" • But cannot change values a = 4 # global space § Assignment to a global makes a new local variable! def show_a(): § Why we limit to constants print(a) # shows global 9/25/18 Memory in Python 14

  15. Function Access to Global Space • All function definitions Global Space a 4 (for globals.py) are in some module • Call can access global change_a space for that module § math.cos : global for math a 3.5 § temperature.to_centigrade # globals.py uses global for temperature """Show how globals work""" • But cannot change values a = 4 # global space § Assignment to a global makes a new local variable! def change_a(): § Why we limit to constants a = 3.5 # local variable 9/25/18 Memory in Python 15

  16. Call Frames and Objects Global Space • Mutable objects can be p altered in a function call id5 § Object vars hold names! Heap Space § Folder accessed by both id5 global var & parameter Point3 • Example : x 0.0 def incr_x(q): … q.x = q.x + 1 Call Frame 1 incr_x 1 >>> p = Point3(0,0,0) q id5 >>> incr_x(p) 9/25/18 Memory in Python 16

  17. Call Frames and Objects Global Space • Mutable objects can be p altered in a function call id5 § Object vars hold names! Heap Space § Folder accessed by both id5 global var & parameter Point3 • Example : x 1.0 x 0.0 def incr_x(q): … q.x = q.x + 1 Call Frame 1 incr_x >>> p = Point3(0,0,0) q id5 >>> incr_x(p) 9/25/18 Memory in Python 17

  18. Call Frames and Objects Global Space • Mutable objects can be p altered in a function call id5 § Object vars hold names! Heap Space § Folder accessed by both id5 global var & parameter Point3 • Example : x 1.0 x 0.0 def incr_x(q): … q.x = q.x + 1 Call Frame 1 ERASE FRAME >>> p = Point3(0,0,0) >>> incr_x(p) 9/25/18 Memory in Python 18

  19. Frames and Helper Functions def last_name_first(s): Call: last_name_first('Walker White'): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Walker White' 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(' ') 1 return s[0:end] 2 9/25/18 Memory in Python 19

  20. Frames and Helper Functions Not done. Do not erase! def last_name_first(s): Call: last_name_first('Walker White'): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Walker White' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 first_name 1 def first_name(s): s 'Walker White' """ Prec : see last_name_first""" end = s.find(' ') 1 return s[0:end] 2 9/25/18 Memory in Python 20

  21. Frames and Helper Functions def last_name_first(s): Call: last_name_first('Walker White'): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Walker White' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 first_name 2 def first_name(s): s 'Walker White' """ Prec : see last_name_first""" end = s.find(' ') end 6 1 return s[0:end] 2 9/25/18 Memory in Python 21

  22. Frames and Helper Functions def last_name_first(s): Call: last_name_first('Walker White'): """ Precondition : s in the form last_name_first 1 <first-name> <last-name>""" s 'Walker White' first = first_name(s) 1 last = last_name(s) 2 return last + ',' + first 3 first_name def first_name(s): s 'Walker White' """ Prec : see last_name_first""" end = s.find(' ') end 6 1 return s[0:end] 2 RETURN 'Walker' 9/25/18 Memory in Python 22

  23. Frames and Helper Functions def last_name_first(s): Call: last_name_first('Walker White'): """ Precondition : s in the form last_name_first 2 <first-name> <last-name>""" s 'Walker White' first = first_name(s) 1 last = last_name(s) 2 first 'Walker' return last + ',' + first 3 ERASE WHOLE FRAME def first_name(s): """ Prec : see last_name_first""" end = s.find(' ') 1 return s[0:end] 2 9/25/18 Memory in Python 23

  24. Frames and Helper Functions def last_name_first(s): Call: last_name_first('Walker White'): """ Precondition : s in the form last_name_first 2 <first-name> <last-name>""" s 'Walker White' first = first_name(s) 1 last = last_name(s) 2 first 'Walker' return last + '.' + first 3 last_name 1 def last_name(s): s 'Walker White' """ Prec : see last_name_first""" end = s.rfind(' ') 1 return s[end+1:] 2 9/25/18 Memory in Python 24

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