Difference between revisions of "Environment"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][unchecked revision]
m ({{Sufficient-class}})
(Incorporate PythonTutor images and explanations)
Line 1: Line 1:
 
{{Sufficient-class}}
 
{{Sufficient-class}}
An '''environment''' consists of a sequence of [[frame]]s.
+
An '''environment''' consists of a sequence of [[frame]]s. Code is evaluated line by line in the environment. Starting in the global frame, users evaluate environments by binding values to variables and by evaluating function calls. Function calls will create local frames, in which the user evaluates the calls (like a mini environment) and returns to the global frame. One way to evaluate an environment is through [[Environment diagram|Environment diagrams]].
  
== Example ==
+
== Examples ==
In the following code:
+
===Text Example===
 +
In the following code, the sequence of frames that makes up the environment of <code>inner</code> is <code>inner</code> → <code>outer</code> → <code>global</code>.
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
x = 1
 
x = 1
Line 16: Line 17:
 
outer()()
 
outer()()
 
</syntaxhighlight>
 
</syntaxhighlight>
the sequence of frames that makes up the environment of <code>inner</code> is <code>inner</code> <code>outer</code> <code>global</code>.
+
# The integer 1 is assigned to variable x.
 +
# 2 is assigned to y.
 +
# The variable outer points to a function.
 +
# The interpreter moves down to <code>outer()()</code>, and executes the function <code>outer()</code>.
 +
## x and y are re-assigned to 3 and 4 respectively.
 +
## The variable inner points to a function (do not execute this function yet!).
 +
## "Return inner" indicates that <code>outer()</code> returns the function <code>inner</code>.
 +
# Since <code>outer()</code> returns <code>inner</code>, now we execute what's left: <code>inner()</code>.
 +
## x and y are re-assigned to 5, 6.
 +
# Program terminates.
 +
 
 +
 
 +
 
 +
===Visual Example===
 +
<gallery mode="packed-hover" heights="180">
 +
File:Env1.png|(Before Execution)
 +
File:Env2.png|(Values assigned to variables)
 +
File:Env3.png|(Functions assigned to variables)
 +
File:Env4.png|(Function call creates new frame)
 +
File:Env5.png|(First function evaluated, next frame created)
 +
File:Env6.png|(Program finished, final state of environment)
 +
</gallery>
  
 
== See also ==
 
== See also ==
 
* [[Environment diagram]]
 
* [[Environment diagram]]

Revision as of 19:45, 26 June 2014

An environment consists of a sequence of frames. Code is evaluated line by line in the environment. Starting in the global frame, users evaluate environments by binding values to variables and by evaluating function calls. Function calls will create local frames, in which the user evaluates the calls (like a mini environment) and returns to the global frame. One way to evaluate an environment is through Environment diagrams.

Examples

Text Example

In the following code, the sequence of frames that makes up the environment of inner is innerouterglobal.

x = 1
y = 2
def outer():
    x = 3
    y = 4
    def inner():
        x = 5
        y = 6
    return inner
outer()()
  1. The integer 1 is assigned to variable x.
  2. 2 is assigned to y.
  3. The variable outer points to a function.
  4. The interpreter moves down to outer()(), and executes the function outer().
    1. x and y are re-assigned to 3 and 4 respectively.
    2. The variable inner points to a function (do not execute this function yet!).
    3. "Return inner" indicates that outer() returns the function inner.
  5. Since outer() returns inner, now we execute what's left: inner().
    1. x and y are re-assigned to 5, 6.
  6. Program terminates.


Visual Example

See also