[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-665407-105":59,"doc-detail-665407-en":130},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","memory-diagrams-tracing-programs-by-hand-environment-diagrams-module-evaluation","Memory Diagrams - Tracing Programs by Hand - Environment Diagrams - Module Evaluation","","Memory Diagrams - Tracing Programs by Hand explains how to predict program behavior by tracking the current line of code, the trail of function calls that led there, and the values of variables with their name-to-location bindings. It introduces environment diagrams for tracing both state and naming environments, including call stack frames and dynamic heap memory. A worked Python example demonstrates frame creation, parameter binding, return-value tracking, and module evaluation with an initial globals frame.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":30,"@type":76,"position":81},"https://docshare.wps.com/document/technology/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/memory-diagrams-tracing-programs-by-hand-environment-diagrams-module-evaluation/665407/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/memory-diagrams-tracing-programs-by-hand-environment-diagrams-module-evaluation/665407.png","ImageObject",300,407,{"name":92,"@type":93},"RizkyPratama_92","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-10-30","2026-10-29",true,{"@type":102,"interactionType":103,"userInteractionCount":81},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"Why do memory diagrams help when evaluating a program?","Question",{"text":112,"@type":113},"Program evaluation depends on many details at once: the next expression to run, the calling path that produced it, and the variable values and their bindings in memory. Diagrams make these moving parts trackable.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"What information is shown in an environment diagram?",{"text":117,"@type":113},"An environment diagram includes call stack frames and dynamic memory heap space. Each frame records the function definition name, variable names with their bound values, and a representation for the return value when the function returns.",{"name":119,"@type":110,"acceptedAnswer":120},"How does Python module evaluation relate to stack and heap in this model?",{"text":121,"@type":113},"When Python loads a module file, the stack and heap are empty outside built-ins. A globals frame is established, evaluation begins from the top of the file, and then function definitions bind names within the current stack frame.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},665407,1793302644,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":29,"category_name":30,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":81,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":139,"language":140,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":67,"update_tm":129,"read_time":144},962094243231,"https://ap-avatar.wpscdn.com/davatar_155a257f0dc6eb9ab79c44ca47cae57d","Memory Diagrams  \nTracing Programs by Hand  \n• Understanding how a program will evaluate depends on systematically keeping track of many details.  \n• As your program is evaluated, there are many moving parts:  \n1. The current line of code, or expression within a line, it will process next  \n2. The trail of function call bookmarks that led to the current line  \n3. The values of all variables and a map of variable \"names\" to the location of their values  \n• For humans, this is more than you can keep track of in your head!  \n• Good news: diagrams will help you keep track of these things... just like the CPU  \nEnvironment Diagrams  \n• A program's state is made up of the values stored in memory.  \n• A program's environment binds names in your program to values in memory.  \n• Use environment diagrams to trace both state and naming environment.  \n• Additionally, they'll help you keep track of how function calls are processed.  \n• In the 2018-2019 academic year we began teaching with these diagrams  \n• On the final exam, students who made use of environment diagrams to trace code were over 50% less likely to make errors than students who did not.  \nEnvironment Diagram  \n• There are two areas of an environment diagram:  \n1. Call Stack (or \"The Stack\")  \n• When a function is called, a new Frame is added  \n• Every frame has:  \n• The name of its function definition  \n• A list of variable names and boxes holding their bound values  \n• Variable values are stored in stack frames  \n• A place to represent its return value (rv) when it returns.  \n2. Dynamic Memory Heap (or \"The Heap\")  \n• We'll come back to this in the next unit.  \n• This is a rough approximation of the model of how state in your programs is managed by the processor.  \nExample:  \nThe Call Stack The Heap  \nEnvironment Diagram Example  \n01 def main() - > None:  \n02 x: int = 4  \n03 y: int = f(x)  \n04 print(x, y) 05  \n06  \n07 def f ( n: int) - > int:  \n08 x: int = n + 1  \n09 return x 10  \n11  \n12 main()  \n• Let's trace the example to the left using an environment diagram!  \n• In the process you will learn how to:  \n• Establish a frame for main  \n• Establish local variables (those declared inside of a function's body) in the frame  \n• Call functions  \n• Establish a frame for the function  \n• Establish parameters as local variables, assigned their argument's values  \n• Keep track of the value returned by a function call  \nEnvironment Diagram Example  \n01 def main() - > None:  \n02 x: int = 4  \n03 y: int = f(x)  \n04 print(x, y) 05  \n06  \n07 def f ( n: int) - > int:  \n08 x: int = n + 1  \n09 return x 10  \n11  \n12 main()  \n• Let's trace the example to the left using an environment diagram!  \n• In the process you will learn how to:  \n• Establish a frame for main  \n• Establish local variables (those declared inside of a function's body) in the frame  \n• Call functions  \n• Establish a frame for the function  \n• Establish parameters as local variables, assigned their argument's values  \n• Keep track of the value returned by a function call  \nModule Evaluation  \nWhen Python loads a module (a file name ending [in .py](in .py)) the stack and heap are empty outside of Python's built-ins, such as the print function, which are outside our diagramming concern.  A Globals frame is established and evaluation begins from the top of the file.   \n01 def main() - > None:  \n02 x: int = 4  \n03 y: int = f(x)  \n04 print(x, y) 05  \n06  \n07 def f ( n: int) - > int:  \n08 x: int = n + 1  \n09 return x 10  \n11  \n12 main()  \nThe Call Stack  \nGlobals  \nThe Heap  \nFunction Definition-main  \nWhen a function is defined, its name is bound in your current stack frame. It refers to an  \nfunction object (\"fn\" shorthand) representing its code stored on the heap. We'll use its line \\#s.  \n01  \n02  \n03  \n04  \n05  \n06  \n07  \n08  \n09  \n10  \n11  \n12  \ndef main() - > None:  \nx: int = 4  \ny: int = f(x) print(x, y)  \ndef f ( n: int) - > int:  \nx: int = n + 1 return x  \nmain()  \nThe Call Stack  \nGlobals  \nmain   \nThe Heap  \nfn: lines","cbCaivd6GteZH5WY","https://ap.wps.com/l/cbCaivd6GteZH5WY","pdf",1012022,34,"English","# Memory Diagrams\n## Tracing Programs by Hand\n## Environment Diagrams\n## Environment Diagram Example\n## Module Evaluation\n## The Call Stack\n## Name Resolution: What is main?","[{\"question\":\"Why do memory diagrams help when evaluating a program?\",\"answer\":\"Program evaluation depends on many details at once: the next expression to run, the calling path that produced it, and the variable values and their bindings in memory. Diagrams make these moving parts trackable.\"},{\"question\":\"What information is shown in an environment diagram?\",\"answer\":\"An environment diagram includes call stack frames and dynamic memory heap space. Each frame records the function definition name, variable names with their bound values, and a representation for the return value when the function returns.\"},{\"question\":\"How does Python module evaluation relate to stack and heap in this model?\",\"answer\":\"When Python loads a module file, the stack and heap are empty outside built-ins. A globals frame is established, evaluation begins from the top of the file, and then function definitions bind names within the current stack frame.\"}]","Memory Diagrams - Tracing Programs by Hand - Environment Diagrams - Module Evaluation | PDF",86]