Environment Diagrams
Environment Diagrams
What are Environment Diagrams? A visual tool to keep track of bindings state of a computer program In this class,we use Python as our primary language o The diagrams we teach can be applied to similar languages
What are Environment Diagrams? ● A visual tool to keep track of bindings & state of a computer program ● In this class, we use Python as our primary language ○ The diagrams we teach can be applied to similar languages
Why do we use Environment Diagrams? Environment Diagrams are conceptual understand why programs work the way they do confidently predict how a program will behave Environment Diagrams are helpful for debugging When you're really stuck, diagramming code staring at lines of code Environment Diagrams will be used in future courses CS 61C (Machine Structures) CS 164 (Programming Languages and Compilers)
Why do we use Environment Diagrams? • Environment Diagrams are conceptual • understand why programs work the way they do • confidently predict how a program will behave • Environment Diagrams are helpful for debugging • When you're really stuck, diagramming code > staring at lines of code • Environment Diagrams will be used in future courses • CS 61C (Machine Structures) • CS 164 (Programming Languages and Compilers)
What do we've seen so far Assignment Statements Global frame X=1 X 3 X =XX+X Def Statements Global frame function def square(x): square(x)】 square return xx square Call Expressions X 4 square(4) Frame Return 16 value
What do we've seen so far Assignment Statements x = 1 x = x + x + x Def Statements def square(x): return x * x Call Expressions square(4) Frame
Terminology:Frames Global frame function square(x) A frame keeps track of varia square Every call expression has a corr square Global,a.k.a.the global fr 4 arting frame. It doesn't correspond to a speci Return 1. 16 Parent frames value The parent of a function is the frame in which it was defined. If you can't find a variable in the current frame,you check it's parent,and so on.If you can't find the variable,NameError Demo
Terminology: Frames A frame keeps track of variable-to-value bindings. ● Every call expression has a corresponding frame. Global, a.k.a. the global frame, is the starting frame. ● It doesn't correspond to a specific call expression. Parent frames ● The parent of a function is the frame in which it was defined. ● If you can't find a variable in the current frame, you check it's parent, and so on. If you can't find the variable, NameError Demo