Memory in Python

[Pages:31]Lecture 10

Memory in Python

Announcements For This Lecture

Reading

? Reread all of Chapter 3

Assignments

? Work on your revisions

? Want done by Sunday

? Survey: 445 responded

? Remaining do by tomorrow ? Avg Time: 6.5 hours ? STD Dev: 4 hours

? Assignment 2 also Sunday

? Scan and submit online

? Assignment 3 up Monday

9/24/15

Memory in Python

2

Modeling Storage in Python

? Global Space

Global Space

? What you "start with" ? Stores global variables ? Also modules & functions!

p

id2

Call Frame

? Lasts until you quit Python

incr_x

? Call Frame

? Variables in function call ? Deleted when call done

? Heap Space

? Where "folders" are stored ? Have to access indirectly

q

id2

id2 x 1.0

Heap Space

Point3

y 2.0 x 3.0

9/24/15

Memory in Python

3

Memory and the Python Tutor

Global Space

Heap Space

Call Frame

9/24/15

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

def to_centigrade(x): return 5*(x-32)/9.0

Global Space

to_centigrade id6

Body

? Variable vs. Call

Heap Space

>>> to_centigrade >>> to_centigrade (32) 0.0

id6

function

Body

9/24/15

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 ? Module functions

? Puts folder id in variable

? from keyword dumps contents to global space

import math

Heap Space

Global Space

math id5

id5

module

pi 3.141592 e 2.718281

functions

9/24/15

Memory in Python

6

Modules vs Objects

Module

math id2 id2

module pi 3.141592 e 2.718281

functions

Object

p id3 id3 x 5.0 Point3 y 2.0 z 3.0

9/24/15

Memory in Python

7

Modules vs Objects

Module

Object

math id2 id2

module

pi 3.141592 e 2.718281

functions math.pi math.cos(1)

p id3 id3 x 5.0 Point

y 2.0

z 3.0 p.x p.clamp(-1,1)

9/24/15

Memory in Python

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download