Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 7 Memory in Python Announcements For This Lecture Readings Assignment 1 • Reread Chapter 3 • No reading for Thursday Lab • Moved to Fri, Sep. 19 § Worried if I push to Sun., people will start too late § Resubmit until Sep. 28 • Work on Assignment 1 • Posted a Survey in CMS § Credit when submit A1 § Questions on A1 § Nothing else to do § Fill it out when done 9/16/14 Python Memory 2 Modeling Storage in Python • Global Space § § § § Global Space What you “start with” Stores global variables Also modules & functions! Lasts until you quit Python p Call Frame incr_x • Call Frame q § Variables in function call § Deleted when call done id2 Heap Space id2 • Heap Space § Where “folders” are stored § Have to access indirectly 9/16/14 id2 Python Memory Point x 1.0 y 2.0 x 3.0 3 Modeling Storage in Python • Global Space § § § § Global Space What you “start with” Stores global variables Also modules & functions! Lasts until you quit Python p Call Frame incr_x • Call Frame q § Variables in function call § Deleted when call done id2 • Heap Space § Where “folders” are stored § Have to access indirectly 9/16/14 id2 Python Memory x id2 Heap rSpace te a l r e v o Will c course Point his 2.0 x 3.0 1.0in ty 4 Memory and the Python Tutor Heap Space Global Space Call Frame 9/16/14 Python Memory 5 Functions and Global Space • A function definition… def to_centigrade(x):" § Creates a global variable (same name as function) return 5*(x-32)/9.0 § Creates a folder for body Global Space 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/16/14 Python Memory 6 Modules and Global Space • Importing a module: § Creates a global variable (same name as module) import math Global Space math id5 Heap Space § Puts contents in a folder id5 • Module variables module • Module functions pi 3.141592 § Puts folder id in variable e 2.718281 • from keyword dumps contents to global space 9/16/14 Python Memory functions 7 Modules vs Objects Module math Object id2 p id2 id3 module pi 3.141592 e 2.718281 functions 9/16/14 id3 x y z Python Memory 5.0 Point 2.0 3.0 8 Modules vs Objects Module math Object id2 p id2 id3 module pi 3.141592 e 2.718281 functions math.pi math.cos(1) 9/16/14 id3 Python Memory x y z 5.0 Point 2.0 3.0 p.x p.clamp(-1,1) 9 Modules vs Objects Module math id2 Object id2 p id3 s n a e m ) . ( d o i r e module The p 5.0 ” r x e d l o f e th 3.141592 “go inside of Point id3 pi e 2.718281 functions math.pi math.cos(1) 9/16/14 Python Memory y z 2.0 3.0 p.x p.clamp(-1,1) 10 Recall: Everything is an Object! • Including basic values x id5 § int, float, bool, str • Example: id5 >>> x = 2.5 >>> id(x) float 2.5 • But basics are immutable § Contents cannot change § Distinction between value and identity is immaterial § So we can ignore the folder 9/16/14 Python Memory x 2.5 11 When Do We Need to Draw a Folder? Yes • Variable holds a § function § module § object § (more????) 9/16/14 No • Variable holds a § base type § bool, int, float, str x id2 id2 Python Memory float 2.5 12 Recall: Call Frames 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Call: to_centigrade(50.0) to_centigrade 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): 1 return 5*(x-32)/9.0 9/16/14 1 Only at the End! Python Memory 13 Recall: Call Frames 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Call: to_centigrade(50.0) to_centigrade § Look for variables in the frame § If not there, look for global variables with that name x 50.0 RETURN 10.0 4. Erase the frame for the call def to_centigrade(x): 1 return 5*(x-32)/9.0 9/16/14 Python Memory 14 Recall: Call Frames 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Call: to_centigrade(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 def to_centigrade(x): 1 return 5*(x-32)/9.0 9/16/14 But don’t actually erase on an exam Python Memory 15 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/16/14 Python Memory 16 Call Frames vs. Global Variables • This does not work: 1 2 3 def swap(a,b): """Swap vars a & b""" tmp = a a = b b = tmp Global Variables a 1 b 2 Call Frame swap 1 >>> a = 1 >>> b = 2 >>> swap(a,b) 9/16/14 a Python Memory 1 b 2 17 Call Frames vs. Global Variables • The specification is false: 1 2 3 def swap(a,b): """Swap vars a & b""" tmp = a a = b b = tmp Global Variables a b 1 2 Call Frame swap 2 >>> a = 1 >>> b = 2 >>> swap(a,b) 9/16/14 a b 1 tmp Python Memory 2 1 18 Call Frames vs. Global Variables • The specification is false: 1 2 3 def swap(a,b): """Swap vars a & b""" tmp = a a = b b = tmp Global Variables a b 1 2 Call Frame swap 3 >>> a = 1 >>> b = 2 >>> swap(a,b) 9/16/14 a 1 2 ✗ tmp Python Memory b 2 1 19 Call Frames vs. Global Variables • The specification is false: 1 2 3 def swap(a,b): """Swap vars a & b""" tmp = a a = b b = tmp Global Variables a b 1 2 Call Frame swap >>> a = 1 >>> b = 2 >>> swap(a,b) 9/16/14 a 1 2 ✗ tmp Python Memory b 2 1 ✗ 1 20 Call Frames vs. Global Variables • The specification is false: 1 2 3 def swap(a,b): """Swap vars a & b""" tmp = a a = b b = tmp Global Variables a 1 b 2 Call Frame >>> a = 1 >>> b = 2 >>> swap(a,b) 9/16/14 Python Memory 21 Function Access to Global Space • All function definitions are in some module • Call can access global space for that module § math.cos: global for math § temperature.to_centigrade uses global for temperature • But cannot change values § Assignment to a global makes a new local variable! § Why we limit to constants 9/16/14 Python Memory Global Space (for globals.py) a 4 show_a 1 # globals.py """Show how globals work""" a = 4 # global space def show_a(): print a # shows global 22 Function Access to Global Space • All function definitions are in some module • Call can access global space for that module § math.cos: global for math § temperature.to_centigrade uses global for temperature • But cannot change values § Assignment to a global makes a new local variable! § Why we limit to constants 9/16/14 Python Memory Global Space (for globals.py) a 4 change_a a 3.5 # globals.py """Show how globals work""" a = 4 # global space def change_a(): a = 3.5 # local variable 23 Call Frames and Objects • Mutable objects can be altered in a function call § Object vars hold names! § Folder accessed by both global var & parameter • Example: def incr_x(q): 1 q.x = q.x + 1 >>> p = Point(0,0,0) >>> incr_x(p) 9/16/14 Global Space p id5 Heap Space id5 x 0.0 … Call Frame incr_x q Python Memory Point 1 id5 24 Call Frames and Objects • Mutable objects can be altered in a function call § Object vars hold names! § Folder accessed by both global var & parameter • Example: def incr_x(q): 1 q.x = q.x + 1 >>> p = Point(0,0,0) >>> incr_x(p) 9/16/14 Global Space p id5 Heap Space id5 Point x 0.0 ✗ 1.0 … Call Frame incr_x q Python Memory id5 25 Call Frames and Objects • Mutable objects can be altered in a function call § Object vars hold names! § Folder accessed by both global var & parameter • Example: def incr_x(q): 1 q.x = q.x + 1 >>> p = Point(0,0,0) >>> incr_x(p) 9/16/14 Global Space p id5 Heap Space id5 Point x 0.0 ✗ 1.0 … Call Frame Python Memory 26