Lecture 0 Memory in Python
Announcements For This Lecture Reading Reread all of Chapter 3 Assignments Work on your revisions Want done by Sunday Survey: 50 responded Remaining do by tomorrow Avg Time: 7.3 hours STD Dev: 3.6 hours Assignment 2 also Sunday Scan and submit online Assignment 3 up Monday 9/2/7 Memory in Python 2
Global Space Modeling Storage in Python What you start with Stores global variables Also modules & functions! Lasts until you quit Python Call Frame Variables in function call Deleted when call done Heap Space Where folders are stored Have to access indirectly Global Space 9/2/7 Memory in Python 3 p incr_x id2 x.0 id2 Call Frame q id2 Heap Space Point3 y 2.0 x 3.0
Memory and the Python Tutor Global Space Heap Space Call Frame 9/2/7 Memory in Python 4
Functions and Global Space A function definition Creates a global variable (same name as function) Creates a folder for body Puts folder id in variable Variable vs. Call >>> to_centigrade <fun to_centigrade at 0x00498de8> >>> to_centigrade (32) 0.0 def to_centigrade(x): return 5*(x-32)/9.0 Global Space to_centigrade id6 Heap Space id6 function Body Body 9/2/7 Memory in Python 5
Modules and Global Space Importing a module: Creates a global variable (same name as module) Puts contents in a folder Module variables import math Heap Space id5 Global Space math id5 module Module functions Puts folder id in variable from keyword dumps contents to global space pi 3.4592 e 2.7828 functions 9/2/7 Memory in Python 6
Modules vs Objects Module Object math id2 p id3 id2 id3 module pi 3.4592 e 2.7828 functions x 5.0 y 2.0 z 3.0 Point3 9/2/7 Memory in Python 7
Modules vs Objects Module Object math id2 p id3 id2 id3 pi 3.4592 e 2.7828 functions module math.pi math.cos() x 5.0 y 2.0 z 3.0 Point3 p.x p.clamp(-,) 9/2/7 Memory in Python 8
Modules vs Objects Module Object math id2 p id3 id2 id3 pi 3.4592 e 2.7828 functions module math.pi math.cos() x 5.0 y 2.0 z 3.0 Point3 p.x p.clamp(-,) 9/2/7 Memory in Python 9
Recall: Call Frames. Draw a frame for the call 2. Assign the argument value 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 Call: to_centigrade(50.0) to_centigrade x 50.0 What is happening here? def to_centigrade(x): return 5*(x-32)/9.0 Only at the End! 9/2/7 Memory in Python 0
Recall: Call Frames. Draw a frame for the call 2. Assign the argument value 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 Call: to_centigrade(50.0) to_centigrade x 50.0 RETURN 0.0 def to_centigrade(x): return 5*(x-32)/9.0 9/2/7 Memory in Python
Recall: Call Frames. Draw a frame for the call 2. Assign the argument value 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 Call: to_centigrade(50.0) def to_centigrade(x): return 5*(x-32)/9.0 But don t actually erase on an exam 9/2/7 Memory in Python 2
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/2/7 Memory in Python 3
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 Global Space (for globals.py) show_a # globals.py """Show how globals work""" a = 4 # global space def show_a(): print(a) # shows global a 4 9/2/7 Memory in Python 4
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 Global Space (for globals.py) change_a a # globals.py 3.5 """Show how globals work""" a = 4 # global space def change_a(): a = 3.5 # local variable a 4 9/2/7 Memory in Python 5
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): q.x = q.x + >>> p = Point3(0,0,0) >>> incr_x(p) Global Space p id5 Heap Space id5 Point3 x 0.0 Call Frame incr_x q id5 9/2/7 Memory in Python 6
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): q.x = q.x + >>> p = Point3(0,0,0) >>> incr_x(p) Global Space p id5 Heap Space id5 x 0.0 x.0 Call Frame incr_x q id5 Point3 9/2/7 Memory in Python 7
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): q.x = q.x + Global Space Call Frame p id5 x id5 Heap Space 0.0 x.0 Point3 >>> p = Point3(0,0,0) >>> incr_x(p) 9/2/7 Memory in Python 8
Frames and Helper Functions 2 3 def last_name_first(s): """Precondition: s in the form <first-name> <last-name>""" first = first_name(s) last = last_name(s) return last + ',' + first Call: last_name_first('walker White'): last_name_first s 'Walker White' 2 def first_name(s): """Prec: see last_name_first""" end = s.find(' ') return s[0:end] 9/2/7 Memory in Python 9
Frames and Helper Functions 2 3 def last_name_first(s): """Precondition: s in the form <first-name> <last-name>""" first = first_name(s) last = last_name(s) return last + ',' + first Not done. Do not erase! Call: last_name_first('walker White'): last_name_first s 'Walker White' 2 def first_name(s): """Prec: see last_name_first""" end = s.find(' ') return s[0:end] first_name s 'Walker White' 9/2/7 Memory in Python 20
Frames and Helper Functions 2 3 def last_name_first(s): """Precondition: s in the form <first-name> <last-name>""" first = first_name(s) last = last_name(s) return last + ',' + first Call: last_name_first('walker White'): last_name_first s 'Walker White' 2 def first_name(s): """Prec: see last_name_first""" end = s.find(' ') return s[0:end] first_name s 'Walker White' end 6 2 9/2/7 Memory in Python 2
Frames and Helper Functions 2 3 2 def last_name_first(s): """Precondition: s in the form <first-name> <last-name>""" first = first_name(s) last = last_name(s) return last + ',' + first def first_name(s): """Prec: see last_name_first""" end = s.find(' ') return s[0:end] Call: last_name_first('walker White'): last_name_first s 'Walker White' first_name s 'Walker White' end 6 RETURN 'Walker' 9/2/7 Memory in Python 22
Frames and Helper Functions 2 3 def last_name_first(s): """Precondition: s in the form <first-name> <last-name>""" first = first_name(s) last = last_name(s) return last + ',' + first Call: last_name_first('walker White'): last_name_first 2 s 'Walker White' first 'Walker' 2 def first_name(s): """Prec: see last_name_first""" end = s.find(' ') return s[0:end] 9/2/7 Memory in Python 23
Frames and Helper Functions 2 3 def last_name_first(s): """Precondition: s in the form <first-name> <last-name>""" first = first_name(s) last = last_name(s) return last + '.' + first Call: last_name_first('walker White'): last_name_first 2 s 'Walker White' first 'Walker' 2 def last_name(s): """Prec: see last_name_first""" end = s.rfind(' ') return s[end+:] last_name s 'Walker White' 9/2/7 Memory in Python 24
The Call Stack Functions are stacked Cannot remove one above w/o removing one below Sometimes draw bottom up (better fits the metaphor) Stack represents memory as a high water mark Must have enough to keep the entire stack in memory Error if cannot hold stack Frame Frame 2 Frame 3 Frame 4 Frame 65 9/2/7 Memory in Python 25
The Call Stack Functions are stacked Cannot remove one above w/o removing one below Sometimes draw bottom up (better fits the metaphor) Stack represents memory as a high water mark Must have enough to keep the entire stack in memory Error if cannot hold stack Frame Frame 2 Frame 3 Frame 4 9/2/7 Memory in Python 26
The Call Stack Functions are stacked Cannot remove one above w/o removing one below Sometimes draw bottom up (better fits the metaphor) Stack represents memory as a high water mark Must have enough to keep the entire stack in memory Error if cannot hold stack Frame Frame 2 Frame 3 Frame 4 Frame 6 9/2/7 Memory in Python 27
The Call Stack Functions are stacked Cannot remove one above w/o removing one below Sometimes draw bottom up (better fits the metaphor) Stack represents memory as a high water mark Must have enough to keep the entire stack in memory Error if cannot hold stack Frame Frame 2 Frame 3 Frame 4 Frame 6 9/2/7 Memory in Python 28
Functions are stacked The Call Stack Cannot Book remove adds one a special above w/o removing frame called one below module. Sometimes Thisdraw WRONG! bottom up (better Module fits the is metaphor) global space Stack represents memory as a high water mark Must have enough to keep the entire stack in memory Error if cannot hold stack Frame Frame 2 Frame 3 Frame 4 Frame 6 9/2/7 Memory in Python 29
Anglicize Example 9/2/7 Memory in Python 30
Anglicize Example Global Space Call Stack 9/2/7 Memory in Python 3