Lecture 10. Memory in Python

Similar documents
Lecture 9. Memory and Call Stacks

Lecture 7. Memory in Python

Lecture 9: Memory in Python

Lecture 5. Defining Functions

Lecture 4. Defining Functions

Lecture 4. Defining Functions

PREPARING FOR PRELIM 1

Announcements for this Lecture

Review 1. Call Frames; Diagramming Objects

CS Prelim 1 Review Fall 2012

CS Prelim 1 Review Fall 2018

Lecture 13. Call Stacks & Debugging

CS Prelim 1 Review Fall 2013

CS Prelim 1 Review Fall 2013

Lecture 4: Defining Functions

CS Prelim 1 Review Fall 2017

CS Prelim 1 Review Fall 2016

CS1110. Lecture 6: Function calls

CS1110. Lecture 6: Function calls

CS 1110 Prelim 1 October 4th, 2012

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

PREPARING FOR THE FINAL EXAM

Lecture 4. Defining Functions

Lecture 8. Reviewing Methods; Wrapper Classes

Lecture 11. Asserts and Error Handling

Lecture 7. Strings & Stepwise Refinement

CS 1110: Introduction to Computing Using Python Lists and Sequences

PREPARING FOR THE FINAL EXAM

CS 1110 Prelim 1 October 17th, 2013

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Announcements for This Lecture

Lecture 12. Lists (& Sequences)

Lecture 11. Asserts and Error Handling

Lecture 8. Conditionals & Control Flow

Lecture 17: Classes (Chapter 15)

Lecture 17: Classes (Chapter 15)

Announcements for This Lecture

Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

Lecture 6: Specifications & Testing

Lecture 10: Lists and Sequences

Conditionals & Control Flow

Lecture 7. Scientific Computation

CS 1110 Prelim 2 November 14th, 2013

Lecture 13. For-Loops

Lecture 3. Functions & Modules

Lecture 3. Functions & Modules

PL Recitation 9/21/2010

CS 1110 Prelim 2 November 12th, 2015

Lecture 3: Functions & Modules

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Lecture 19. Using Classes Effectively

Lecture 11: Iteration and For-Loops

61A Lecture 7. Monday, September 16

PREPARING FOR PRELIM 2

CS 1110 Prelim 1 October 15th, 2015

Programming Languages and Techniques (CIS120)

CS61A Lecture 32. Amir Kamil UC Berkeley April 5, 2013

Lecture 19. Operators and Abstraction

Lecture 18. Using Classes Effectively

Lecture 14. Nested Lists and Dictionaries

61A Lecture 7. Monday, September 15

CS1 Lecture 12 Feb. 11, 2019

CS Lecture 26: Grab Bag. Announcements

CS 1110 Prelim 2 November 13th, 2014

Announcements For This Lecture

CS 1110 Prelim 2 November 10th, 2016

CS1 Lecture 13 Feb. 13, 2019

Announcements For This Lecture

CS1110 Lab 6 (Mar 17-18, 2015)

Unit 10: Data Structures CS 101, Fall 2018

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Lecture 21. Programming with Subclasses

CS 1110 Prelim 2 November 6th, 2012

CS 1110 Final, December 17th, Question Points Score Total: 100

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

Data Structures and Algorithms

Intro to Computer Science Project - Address Book 2

Lecture 13. For-Loops

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

ECS Baruch Lab 5 Spring 2019 Name NetID (login, like , not your SUID)

Programming for Engineers in Python

CS Prelim 2 Review Fall 2018

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

CS 1110 Prelim 2 November 12th, 2015

CSE : Python Programming

CS 1110 Prelim 1 October 12th, 2017

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Programming Languages and Techniques (CIS120)

Iteration and For Loops

CS1 Lecture 5 Jan. 26, 2018

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Announcements for the Class

Lecture 21. Programming with Subclasses

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

15-110: Principles of Computing, Spring Problem Set 2 (PS2) Due: Friday, February 2 by 2:30PM on Gradescope

COMP1730/COMP6730 Programming for Scientists. Functions

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Transcription:

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