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

memory in python announcements for this lecture
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Memory in Python

Lecture 10

slide-2
SLIDE 2

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

2 9/25/18 Memory in Python

slide-3
SLIDE 3

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

id2

p

id2 1.0

Point3

x incr_x id2 q

Global Space Call Frame

2.0 y 3.0 x

Heap Space

slide-4
SLIDE 4

Memory and the Python Tutor

9/25/18 Memory in Python 4

Global Space Call Frame Heap Space

slide-5
SLIDE 5

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

id6 to_centigrade

Heap Space

id6

Body

function

Body

slide-6
SLIDE 6

Modules and Global Space

import math

9/25/18 Memory in Python 6

Global Space

id5 math

Heap Space

id5

module

  • 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

slide-7
SLIDE 7

Modules vs Objects

Module Object

9/25/18 Memory in Python 7

id3 x 5.0 y 2.0 z 3.0 id3 p Point3 id2 id2 math module pi 3.141592 e 2.718281

functions

slide-8
SLIDE 8

Modules vs Objects

Module Object

9/25/18 Memory in Python 8

id3 x 5.0 y 2.0 z 3.0 id3 p Point3 id2 id2 math module pi 3.141592 e 2.718281

functions

math.pi math.cos(1) p.x p.clamp(-1,1)

slide-9
SLIDE 9

Modules vs Objects

Module Object

9/25/18 Memory in Python 9

id3 x 5.0 y 2.0 z 3.0 id3 p Point3 id2 id2 math module pi 3.141592 e 2.718281

functions

math.pi math.cos(1) p.x p.clamp(-1,1)

The period (.) means “go inside of the folder”

slide-10
SLIDE 10

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

§ 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.0 x

1

Call: to_centigrade(50.0)

Only at the End! What is happening here?

slide-11
SLIDE 11

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

§ 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.0 x

1

Call: to_centigrade(50.0)

RETURN

10.0

slide-12
SLIDE 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

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

1

Call: to_centigrade(50.0)

ERASE WHOLE FRAME

But don’t actually erase on an exam

slide-13
SLIDE 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

slide-14
SLIDE 14

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 constants

9/25/18 Memory in Python 14

show_a 1 4 a

# globals.py """Show how globals work""" a = 4 # global space def show_a(): print(a) # shows global

slide-15
SLIDE 15

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 constants

9/25/18 Memory in Python 15

change_a 3.5 a 4 a

# globals.py """Show how globals work""" a = 4 # global space def change_a(): a = 3.5 # local variable

slide-16
SLIDE 16

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 id5 q

Call Frame

id5 0.0 …

Point3

x

Global Space

id5 p

Heap Space

slide-17
SLIDE 17

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 id5 q

Call Frame

id5 0.0 …

Point3

x

Global Space

id5 p

Heap Space

x

1.0

slide-18
SLIDE 18

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

id5 p

Heap Space

x

ERASE FRAME 1.0

slide-19
SLIDE 19

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

1 2 3 1 2

Call: last_name_first('Walker White'): last_name_first 1 'Walker White'

s

slide-20
SLIDE 20

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

1 2 3 1 2

Call: last_name_first('Walker White'): last_name_first 1 'Walker White'

s

first_name 'Walker White'

s

1

Not done. Do not erase!

slide-21
SLIDE 21

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

1 2 3 1 2

Call: last_name_first('Walker White'): last_name_first 1 'Walker White'

s

first_name 'Walker White'

s end

6 2

slide-22
SLIDE 22

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'

1 2 3 1 2

s

first_name 'Walker White'

s end

6 Call: last_name_first('Walker White'): RETURN 'Walker'

slide-23
SLIDE 23

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

1 2 3 1 2

first

'Walker'

ERASE WHOLE FRAME

Call: last_name_first('Walker White'):

slide-24
SLIDE 24

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

1 2 3 1 2

Call: last_name_first('Walker White'): last_name_first 2 'Walker White'

s

last_name 'Walker White'

s first

'Walker' 1

slide-25
SLIDE 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

9/25/18 Memory in Python 25

Frame 1 Frame 2 Frame 3 Frame 4 Frame 6 Frame 5 calls calls calls calls

slide-26
SLIDE 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

9/25/18 Memory in Python 26

Frame 1 Frame 2 Frame 3 Frame 4 calls calls calls

slide-27
SLIDE 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

9/25/18 Memory in Python 27

Frame 1 Frame 2 Frame 3 Frame 4 Frame 6 calls calls calls calls

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

Anglicize Example

9/25/18 Memory in Python 30

slide-31
SLIDE 31

Anglicize Example

9/25/18 Memory in Python 31

Global Space Call Stack