Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture...
Transcript of Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture...
![Page 1: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/1.jpg)
Memory in Python
Lecture 10
![Page 2: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/2.jpg)
Announcements For This Lecture
Assignment 1• Work on your revisions
§ Read feedback carefully§ Want done by tomorrow§ Partial credit after Wed.
• Survey: 660 responded§ Deadline is tomorrow§ Avg Time: 6.8 hours!§ STD Dev: 3.9 hours
More Assignments• Assignment 2 TONIGHT
§ Scan and submit online§ Upload before midnight§ Late: -10% per day§ No lates after Thursday
• Assignment 3 is posted§ Due week from Friday§ Before you go on Fall Break§ Graded when you get back
29/25/18 Memory in Python
![Page 3: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/3.jpg)
Modeling Storage in Python
• Global Space§ 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
9/25/18 Memory in Python 3
id2p
id2
1.0
Point3
x
incr_x
id2q
Global Space
Call Frame
2.0y 3.0x
Heap Space
![Page 4: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/4.jpg)
Memory and the Python Tutor
9/25/18 Memory in Python 4
Global Space
Call Frame
HeapSpace
![Page 5: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/5.jpg)
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 0x100498de8>>>> to_centigrade (32)0.0
def to_centigrade(x):return 5*(x-32)/9.0
9/25/18 Memory in Python 5
Global Space
id6to_centigrade
Heap Space
id6
Body
function
Body
![Page 6: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/6.jpg)
Modules and Global Space
import math
9/25/18 Memory in Python 6
Global Space
id5mathHeap Space
id5module
• Importing a module:§ Creates a global variable
(same name as module)
§ Puts contents in a folder• Module variables
• Module functions
§ Puts folder id in variable
• from keyword dumps contents to global space
pi 3.141592
e 2.718281
functions
![Page 7: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/7.jpg)
Modules vs Objects
Module Object
9/25/18 Memory in Python 7
id3
x 5.0
y 2.0
z 3.0
id3p
Point3
id2
id2math
module
pi 3.141592
e 2.718281
functions
![Page 8: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/8.jpg)
Modules vs Objects
Module Object
9/25/18 Memory in Python 8
id3
x 5.0
y 2.0
z 3.0
id3p
Point3
id2
id2math
module
pi 3.141592
e 2.718281
functions math.pimath.cos(1)
p.xp.clamp(-1,1)
![Page 9: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/9.jpg)
Modules vs Objects
Module Object
9/25/18 Memory in Python 9
id3
x 5.0
y 2.0
z 3.0
id3p
Point3
id2
id2math
module
pi 3.141592
e 2.718281
functions math.pimath.cos(1)
p.xp.clamp(-1,1)
The period (.) means
“go inside of the folder”
![Page 10: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/10.jpg)
Recall: Call Frames
1. Draw a frame for the call2. 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
9/25/18 Memory in Python 10
def to_centigrade(x):return 5*(x-32)/9.0
to_centigrade 1
50.0x
1
Call: to_centigrade(50.0)
Only at the End!
What is happening here?
![Page 11: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/11.jpg)
Recall: Call Frames
1. Draw a frame for the call2. 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
9/25/18 Memory in Python 11
def to_centigrade(x):return 5*(x-32)/9.0
to_centigrade
50.0x
1
Call: to_centigrade(50.0)
RETURN 10.0
![Page 12: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/12.jpg)
Recall: Call Frames
1. Draw a frame for the call2. 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
9/25/18 Memory in Python 12
def to_centigrade(x):return 5*(x-32)/9.01
Call: to_centigrade(50.0)ERASE WHOLE FRAME
But don’t actually erase on an exam
![Page 13: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/13.jpg)
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
![Page 14: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/14.jpg)
Global Space (for globals.py)
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 constants9/25/18 Memory in Python 14
show_a 1
4a
# globals.py"""Show how globals work"""a = 4 # global space
def show_a():print(a) # shows global
![Page 15: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/15.jpg)
Global Space (for globals.py)
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 constants9/25/18 Memory in Python 15
change_a
3.5a
4a
# globals.py"""Show how globals work"""a = 4 # global space
def change_a():a = 3.5 # local variable
![Page 16: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/16.jpg)
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 + 1
>>> p = Point3(0,0,0)>>> incr_x(p)
9/25/18 Memory in Python 16
1
incr_x 1
id5q
Call Frame
id5
0.0…
Point3
x
Global Space id5p
Heap Space
![Page 17: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/17.jpg)
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 + 1
>>> p = Point3(0,0,0)>>> incr_x(p)
9/25/18 Memory in Python 17
1
incr_x
id5q
Call Frame
id5
0.0…
Point3
x
Global Space id5p
Heap Space
x 1.0
![Page 18: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/18.jpg)
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 + 1
>>> p = Point3(0,0,0)>>> incr_x(p)
9/25/18 Memory in Python 18
1 Call Frame
id5
0.0…
Point3
x
Global Space id5p
Heap Space
x
ERASE FRAME
1.0
![Page 19: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/19.jpg)
Frames and Helper Functions
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]
9/25/18 Memory in Python 19
123
12
Call: last_name_first('Walker White'):last_name_first 1
'Walker White's
![Page 20: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/20.jpg)
Frames and Helper Functions
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]
9/25/18 Memory in Python 20
123
12
Call: last_name_first('Walker White'):last_name_first 1
'Walker White's
first_name
'Walker White's
1
Not done. Do not erase!
![Page 21: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/21.jpg)
Frames and Helper Functions
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]
9/25/18 Memory in Python 21
123
12
Call: last_name_first('Walker White'):last_name_first 1
'Walker White's
first_name
'Walker White's
end 6
2
![Page 22: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/22.jpg)
Frames and Helper Functions
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]
9/25/18 Memory in Python 22
last_name_first 1
'Walker White'123
12
s
first_name
'Walker White's
end 6
Call: last_name_first('Walker White'):
RETURN 'Walker'
![Page 23: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/23.jpg)
last_name_first 2
'Walker White's
Frames and Helper Functions
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]
9/25/18 Memory in Python 23
123
12
first 'Walker'
ERASE WHOLE FRAME
Call: last_name_first('Walker White'):
![Page 24: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/24.jpg)
Frames and Helper Functions
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 last_name(s):"""Prec: see last_name_first""" end = s.rfind(' ')return s[end+1:]
9/25/18 Memory in Python 24
123
12
Call: last_name_first('Walker White'):last_name_first 2
'Walker White's
last_name
'Walker White's
first 'Walker'
1
![Page 25: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/25.jpg)
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
9/25/18 Memory in Python 25
Frame 1
Frame 2
Frame 3
Frame 4
Frame 6Frame 5
calls
calls
calls
calls
![Page 26: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/26.jpg)
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
9/25/18 Memory in Python 26
Frame 1
Frame 2
Frame 3
Frame 4
calls
calls
calls
![Page 27: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/27.jpg)
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
9/25/18 Memory in Python 27
Frame 1
Frame 2
Frame 3
Frame 4
Frame 6
calls
calls
calls
calls
![Page 28: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/28.jpg)
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
9/25/18 Memory in Python 28
Frame 1
Frame 2
Frame 3
Frame 4
Frame 6
calls
calls
calls
calls
![Page 29: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/29.jpg)
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
9/25/18 Memory in Python 29
Frame 1
Frame 2
Frame 3
Frame 4
Frame 6
calls
calls
calls
calls
Book adds a special “frame” called module.
This is WRONG!Module is global space
![Page 30: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/30.jpg)
Anglicize Example
9/25/18 Memory in Python 30
![Page 31: Memory in Python - cs.cornell.edu · Memory in Python Lecture 10. Announcements For This Lecture Assignment 1 •Work on your revisions §Read feedback carefully §Want done by tomorrow](https://reader035.fdocuments.us/reader035/viewer/2022062402/5f8433545be46c1d2253044b/html5/thumbnails/31.jpg)
Anglicize Example
9/25/18 Memory in Python 31
Global Space
Call Stack