memory in python announcements for this lecture
play

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


  1. Lecture 10 Memory in Python

  2. 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 § Late: -10% per day § Partial credit after Wed. § No lates after THURS • Survey : 676 responded • Assignment 3 is posted § Deadline is tomorrow § Due week from Friday § Avg Time : 6.5 hours! § Before you go on Fall Break § STD Dev : 3.6 hours § Graded when you get back 10/1/19 Memory in Python 2

  3. The Three “Areas” of Memory The Heap Global Space Call Stack 10/1/19 Memory in Python 3

  4. Global Space • This is the area you “start with” § First memory area you learned to visualize § A place to store “global variables” § Lasts until you quit Python p id2 • What are global variables ? § Any assignment not in a function definition § Also modules & functions! § Will see more on this in a bit 10/1/19 Memory in Python 4

  5. The Call Stack • The area where call frames live § Call frames are created on a function call § May be several frames (functions call functions) § Each frame deleted as the call completes • Area of volatile, temporary memory incr_x 2 § Less permanent than global space § Think of as “scratch” space q id2 • Primary focus of Assignment 2 10/1/19 Memory in Python 5

  6. Heap Space or “The Heap” • Where the “folders” live § Stores only folders id2 • Can only access indirectly Point3 § Must have a variable with identifier § Can be in global space, call stack 0.0 x • MUST have variable with id 0.0 y § If no variable has id, it is forgotten 0.0 z § Disappears in Tutor immediately § But not necessarily in practice § Role of the garbage collector 10/1/19 Memory in Python 6

  7. Everything is an Object! • Last time we saw that everything is an object § Must have a folder in the heap § Must have variable in global space, call stack § But ignore basic types ( int , float , bool , str ) • Includes modules and function definitions ! § Object is created by import § Object is created by def § Already seen this in Python Tutor 10/1/19 Memory in Python 7

  8. Modules and Global Space import math • Importing a module: § Creates a global variable Global Space (same name as module) math id5 Heap Space § Puts contents in a folder id5 • Module variables module • Module functions 3.141592 pi § Puts folder id in variable 2.718281 e • from keyword dumps functions contents to global space 10/1/19 Memory in Python 8

  9. Modules vs Objects Module Object math id2 p id3 id2 id3 module Point3 5.0 x 3.141592 pi 2.0 y 2.718281 e 3.0 z math.pi p.x functions math.cos(1) p.clamp(-1,1) 10/1/19 Memory in Python 9

  10. Modules vs Objects Module Object math id2 p id3 id2 id3 The period (.) means module Point3 “go inside of the folder” 5.0 x 3.141592 pi 2.0 y 2.718281 e 3.0 z math.pi p.x functions math.cos(1) p.clamp(-1,1) 10/1/19 Memory in Python 10

  11. So Why Have Both? • Question is a matter of program design § Some software will use modules like objects • Classes can have many instances § Infinitely many objects for the Point3 class § Reason we need a constructor function • Each module is a unique instance § Only one possibility for pi , cosine § That is why we import them § Sometimes refer to as singleton objects 10/1/19 Memory in Python 11

  12. So Why Have Both? • Question is a matter of program design § Some software will use modules like objects • Classes can have many instances c i p o § Infinitely many objects for the Point3 class t d e c n a v d a n a e s s i r u e o c § Reason we need a constructor function i c o s h i C h t f o e p o c s d n o y e b • Each module is a unique instance § Only one possibility for pi , cosine § That is why we import them § Sometimes refer to as singleton objects 10/1/19 Memory in Python 12

  13. How About import * ? Ouch! 10/1/19 Memory in Python 13

  14. Functions and Global Space • A function definition … def to_centigrade(x): Body § Creates a global variable return 5*(x-32)/9.0 (same name as function) Global Space § Creates a folder for body to_centigrade id6 § Puts folder id in variable • Variable vs. Call Heap Space >>> to_centigrade id6 <fun to_centigrade at 0x100498de8> function >>> to_centigrade (32) Body 0.0 10/1/19 Memory in Python 14

  15. Working with Function Variables • So function definitions are objects § Function names are just variables § Variable refers to a folder storing the code § If you reassign the variable, it is lost • You can assign them to other variables § Variable now refers to that function § You can use that NEW variable to call it § Just use variable in place of function name 10/1/19 Memory in Python 15

  16. Example: add_one Frame remembers the original name 10/1/19 Memory in Python 16

  17. Example: add_one c i p o t d e c n a v d a n a e s s r i u e o g c a s s U i h t f o e p o c s d n o y e b Frame remembers the original name 10/1/19 Memory in Python 17

  18. Why Show All This? • Many of these are advanced topics § Only advanced programmers need § Will never need in the context of 1110 • But you might use them by accident • Goal: Teach you to read error messages § Need to understand what messages say § Only way to debug your own code § This means understanding the call stack 10/1/19 Memory in Python 18

  19. Recall: Call Frames Call : to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to_centigrade 1 to the parameter (in frame) 3. Execute the function body x 50.0 § Look for variables in the frame § If not there, look for global variables with that name 4. Erase the frame for the call def to_centigrade(x): return 5*(x-32)/9.0 1 10/1/19 Memory in Python 19

  20. 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 10/1/19 Memory in Python 20

  21. Recall: Call Frames Call : to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to_centigrade 1 to the parameter (in frame) 3. Execute the function body x 50.0 § Look for variables in the frame § If not there, look for global variables with that name 4. Erase the frame for the call What is happening here? def to_centigrade(x): return 5*(x-32)/9.0 1 10/1/19 Memory in Python 21

  22. Function Access to Global Space • Consider code to right Global Space a 4 (for globals.py) § Global variable a § Function definition get_a get_a 6 • Consider the call get_a() § Call frame to the right § What happens? # globals.py """Show how globals work""" A: It crashes a = 4 # global space B: Returns None C: Returns 4 def get_a(): D: I don’t know return a 10/1/19 Memory in Python 22

  23. Function Access to Global Space • Consider code to right Global Space a 4 (for globals.py) § Global variable a § Function definition get_a get_a 6 • Consider the call get_a() § Call frame to the right § What happens? # globals.py """Show how globals work""" A: It crashes a = 4 # global space B: Returns None C: Returns 4 CORRECT def get_a(): D: I don’t know return a 10/1/19 Memory in Python 23

  24. Function Access to Global Space • All function definitions Global Space a 4 (for globals.py) are in some module • Call can access global get_a 6 space for that module § math.cos : global for math § temperature.to_centigrade # globals.py uses global for temperature """Show how globals work""" • But cannot change values a = 4 # global space § Makes a new local variable ! def get_a(): § Why we limit to constants return a 10/1/19 Memory in Python 24

  25. Function Access to Global Space • All function definitions Global Space a 4 (for globals.py) are in some module • Call can access global change_a space for that module a 3.5 § math.cos : global for math § temperature.to_centigrade # globals.py uses global for temperature """Show how globals work""" • But cannot change values a = 4 # global space § Makes a new local variable ! def change_a(): § Why we limit to constants a = 3.5 # local variable 10/1/19 Memory in Python 25

  26. Frames and Helper Functions Call: last_name_first('Walker White'): 1. def last_name_first(s): """ Precond : s in the form 2. last_name_first 4 3. 'first-name last-name' """ s 'Walker White' 4. first = first_name(s) 5. last = last_name(s) 6. return last + ',' + first 7. 8. def first_name(s): """ Precond : see above""" 9. 10. end = s.find(' ') 11. return s[0:end] 10/1/19 Memory in Python 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend