Lecture 7. Memory in Python

Similar documents
Lecture 9. Memory and Call Stacks

Lecture 10. Memory in Python

Announcements for this Lecture

Lecture 4. Defining Functions

Lecture 5. Defining Functions

Lecture 4. Defining Functions

Lecture 4: Defining Functions

PREPARING FOR PRELIM 1

Lecture 3. Functions & Modules

Lecture 3. Functions & Modules

Lecture 4. Defining Functions

CS1110. Lecture 6: Function calls

CS1110. Lecture 6: Function calls

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

Lecture 3: Functions & Modules

Announcements for This Lecture

Lecture 10: Lists and Sequences

CS 1110 Prelim 1 October 17th, 2013

Lecture 12. Lists (& Sequences)

Lecture 3. Strings, Functions, & Modules

Lecture 8. Conditionals & Control Flow

Announcements for This Lecture

Lecture 17: Classes (Chapter 15)

Lecture 13. For-Loops

CS 1110: Introduction to Computing Using Python Lists and Sequences

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

CS 1110 Prelim 1 October 4th, 2012

PREPARING FOR THE FINAL EXAM

Lecture 17: Classes (Chapter 15)

CIS192 Python Programming. Robert Rand. August 27, 2015

Lecture 19. Operators and Abstraction

Announcements. Lecture Agenda. Class Exercise. Hashable. Mutability. COMP10001 Foundations of Computing Iteration

Reasoning About Imperative Programs. COS 441 Slides 10

Lecture 9: Memory in Python

Introduction to Python

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

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

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

Lecture 13. For-Loops

PREPARING FOR THE FINAL EXAM

Conditionals & Control Flow

Lecture 14. Nested Lists and Dictionaries

Iteration and For Loops

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

CS 1110 Prelim 1 October 15th, 2015

Here n is a variable name. The value of that variable is 176.

cs1114 REVIEW of details test closed laptop period

Fall 08, Sherri Goings, Exam #1 (10/2), form 1 B

CS Prelim 2 Review Fall 2018

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

CSCI 2041: Functions, Mutation, and Arrays

CS Prelim 1 Review Fall 2016

1.00 Tutorial 1. Introduction to 1.00

CS1 Lecture 3 Jan. 22, 2018

CSE 115. Introduction to Computer Science I

Programming for Engineers in Python. Recitation 1

Lecture 7. Scientific Computation

Downloaded from Chapter 2. Functions

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Lecture 2. Variables & Assignment

61A Lecture 7. Monday, September 15

Programming Languages and Techniques (CIS120)

Lecture 19. Using Classes Effectively

Lecture 11: Iteration and For-Loops

CIS192: Python Programming

Lecture 20. Subclasses & Inheritance

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

Lecture 18. Using Classes Effectively

Programming for Engineers in Python. Autumn

Copied from: on 3/20/2017

ENGR 102 Engineering Lab I - Computation

Lecture 2: Variables & Assignments

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

somedata = { } somedata[ cheese ] = dairy somedata[ Cheese ] = dairy items = ( ( 3, 2 ), ( 5, 7 ), ( 1, 9 ), 0, ( 1 ) )

Algorithms and Programming I. Lecture#12 Spring 2015

Data Structures (list, dictionary, tuples, sets, strings)

CS1 Lecture 3 Jan. 18, 2019

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Lecture 20: templates

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

CS 1110 Final, December 16th, 2013

Announcements For This Lecture

A list is a mutable heterogeneous sequence

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Python Programming Exercises 1

Beyond Blocks: Python Session #1

Lecture #23: Conversion and Type Inference

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

CS 1110 Final, December 16th, 2013

CS61B Lecture #7. Announcements:

Accelerating Information Technology Innovation

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Lecture 18. Methods and Operations

PREPARING FOR PRELIM 2

Midterm II CS164, Spring 2006

Transcription:

Lecture 7 Memory in Python

Announcements For This Lecture Readings Reread Chapter 3 No reading for Thursday Lab Work on Assignment Credit when submit A Nothing else to do Assignment Moved to Fri, Sep. 9 Worried if I push to Sun., people will start too late Resubmit until Sep. 28 Posted a Survey in CMS Questions on A Fill it out when done 9/6/4 Python Memory 2

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 Global Space 9/6/4 Python Memory 3 p incr_x id2 x.0 id2 Call Frame q id2 Heap Space Point y 2.0 x 3.0

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 Global Space 9/6/4 Python Memory 4 p incr_x id2 x q.0 id2 Call Frame id2 Heap Space Will cover later in this course Point y 2.0 x 3.0

Memory and the Python Tutor Global Space Heap Space Call Frame 9/6/4 Python Memory 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 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/6/4 Python Memory 6

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/6/4 Python Memory 7

Modules vs Objects Module Object math id2 id2 module pi 3.4592 e 2.7828 functions p id3 x 5.0 y 2.0 z 3.0 id3 Point 9/6/4 Python Memory 8

Modules vs Objects Module Object id2 math pi 3.4592 e 2.7828 functions id2 module math.pi math.cos() p id3 x 5.0 y 2.0 z 3.0 id3 Point p.x p.clamp(-,) 9/6/4 Python Memory 9

Modules vs Objects Module Object id2 math pi 3.4592 e 2.7828 functions id2 module id3 The period (.) means x 5.0 go inside of the folder math.pi math.cos() y 2.0 z 3.0 p id3 Point p.x p.clamp(-,) 9/6/4 Python Memory 0

Recall: Everything is an Object! Including basic values int, float, bool, str Example: >>> x = 2.5 >>> id(x) But basics are immutable Contents cannot change Distinction between value and identity is immaterial So we can ignore the folder x id5 x id5 2.5 float 2.5 9/6/4 Python Memory

When Do We Need to Draw a Folder? Yes Variable holds a function module object (more????) No Variable holds a base type bool, int, float, str x id2 id2 float 2.5 9/6/4 Python Memory 2

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/6/4 Python Memory 3

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/6/4 Python Memory 4

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/6/4 Python Memory 5

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/6/4 Python Memory 6

Call Frames vs. Global Variables This does not work: def swap(a,b): 2 """Swap vars a & b""" tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame 3 swap a b 2 9/6/4 Python Memory 7

Call Frames vs. Global Variables The specification is false: def swap(a,b): """Swap vars a & b""" 2 3 tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame swap 2 a b 2 tmp 9/6/4 Python Memory 8

Call Frames vs. Global Variables The specification is false: def swap(a,b): """Swap vars a & b""" 2 3 tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame swap 3 a 2 b 2 tmp 9/6/4 Python Memory 9

Call Frames vs. Global Variables The specification is false: def swap(a,b): """Swap vars a & b""" 2 3 tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame swap 2 a b 2 tmp 9/6/4 Python Memory 20

Call Frames vs. Global Variables The specification is false: def swap(a,b): """Swap vars a & b""" 2 3 tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame 9/6/4 Python Memory 2

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 a """Show how globals work""" a = 4 # global space def show_a(): print a # shows global 4 9/6/4 Python Memory 22

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 a """Show how globals work""" a = 4 # global space def change_a(): a = 3.5 # local variable 4 9/6/4 Python Memory 23

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 = Point(0,0,0) >>> incr_x(p) Global Space p id5 Heap Space id5 Point x 0.0 Call Frame incr_x q id5 9/6/4 Python Memory 24

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 = Point(0,0,0) >>> incr_x(p) Global Space p id5 Heap Space id5 x 0.0.0 Call Frame incr_x q id5 Point 9/6/4 Python Memory 25

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 = Point(0,0,0) >>> incr_x(p) Global Space p id5 Heap Space id5 x 0.0.0 Call Frame Point 9/6/4 Python Memory 26