04-Environments 4pp

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Environments Announcements

Types of expressions

An expression describes a computation and evaluates to a value

18 + 69 log2 1024
6 sin ⇡
Expressions
23
2100 p
f (x) 3493161
100
X 1
7 mod 2 lim
i ✓ ◆ x!1 x
i=1
69
| 1869| 18
(Demo)
4
Anatomy of a Call Expression Evaluating Nested Expressions

add ( 2 + 2 , mul(3, 4) )
224
Operator Operand Operand mul(add(4, mul(4, 6)), add(3, 5))

Operators and operands are also expressions


mul 28 8
add(4, mul(4, 6)) add(3, 5)
So they evaluate to values

Evaluation procedure for call expressions: add 4 24 add 3 5

mul(4, 6)
1. Evaluate the operator and then the operand subexpressions

2. Apply the function that is the value of the operator mul 4 6

to the arguments that are the values of the operands

5 6

Evaluating Nested Expressions

Value of the whole expression


Operand subexpression
224
mul(add(4, mul(4, 6)), add(3, 5))

Value of subexpression 1st argument to mul


mul 28 8
add(4, mul(4, 6)) add(3, 5) Print and None

add 4 24 add 3 5
(Demo)
mul(4, 6)

mul 4 6

Expression tree

7
None Indicates that Nothing is Returned Pure Functions & Non-Pure Functions

The special value None represents nothing in Python


Return value
Pure Functions
just return values -2 abs
A function that does not explicitly return a value will return None
2
Argument
Careful: None is not displayed by the interpreter as the value of an expression

2, 100 pow
>>> def does_not_return_square(x): 1267650600228229401496703205376
... x * x 2 Arguments
No return
...
None value is not displayed Returns None!
>>> does_not_return_square(4) Non-Pure Functions
have side effects -2 print
The name sixteen >>> sixteen = does_not_return_square(4) None
is now bound to A side effect isn't a
the value None >>> sixteen + 4 A non-pure value; it's anything
Traceback (most recent call last): function doesn't Python displays the output “-2” that happens as a
have to return consequence of
File "<stdin>", line 1, in <module> None (but print calling a function
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' always does).

9 10

Nested Expressions with Print

None, None print(...):


None Does not get
displayed

display “None None”


None
print(print(1), print(2))
Names, Assignment, and User-Defined Functions
func print(...) None None
print(1) print(2)

func print(...) 1 func print(...) 2


(Demo)
1 print(...): 2 print(...):
None None

display “1” display “2”

11
Environment Diagrams

Environment diagrams visualize the interpreter’s process.

Just executed Import statement

Environment Diagrams Name Value

Next to execute Assignment statement

Code (left): Frames (right):

Statements and expressions Each name is bound to a value

Arrows indicate evaluation order Within a frame, a name cannot be repeated

(Demo: tutor.cs61a.org )

http://pythontutor.com/composingprograms.html#code=from%20math%20import%20pi%0Atau%20%3D%202%20*%20pi&cumulative=false&curInstr=1&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
14

Assignment Statements

Just executed

Next to execute

Calling Functions

Just executed

Execution rule for assignment statements:

(Demo: tutor.cs61a.org )
1. Evaluate all expressions to the right of = from left to right.

2. Bind all names to the left of = to those resulting values in the current frame.

http://pythontutor.com/composingprograms.html#code=a%20%3D%201%0Ab%20%3D%202%0Ab,%20a%20%3D%20a%20%2B%20b,%20b&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
15
http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
Calling User-Defined Functions Calling User-Defined Functions
Procedure for calling/applying user-defined functions (version 1): Procedure for calling/applying user-defined functions (version 1):
1. Add a local frame 1. Add a local frame
2. Bind the function's formal parameters to its arguments in that frame 2. Bind the function's formal parameters to its arguments in that frame
3. Execute the body of the function in that new environment 3. Execute the body of the function in that new environment

Built-in function

Original name of
function called

User-defined
Local frame function

A function’s signature has all the


Formal parameter information needed to create a local frame
bound to argument
Return value
(not a binding!)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
17 http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
18

Life Cycle of a User-Defined Function Multiple Environments in One Diagram!


What happens?
Formal parameter
Return
Name expression
Def statement: >>> def square( x ): A new function is created!

return mul(x, x) Name bound to that function


Def
statement in the current frame
Body (return statement)

operand: 2+2 Operator & operands evaluated


Call expression: square(2+2) argument: 4
Function (value of operator)
called on arguments
operator: square (values of operands)
function: func square(x) square(square(3))

A new frame is created! func square(x)


Calling/Applying: 4 square( x ):
square(3)
Parameters bound to arguments
Argument 16
Signature
Body is executed func square(x) 3
Return value
19 http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
20
Multiple Environments in One Diagram! Multiple Environments in One Diagram!

1
81
square(square(3)) square(square(3))

func square(x) 9 func square(x) 9


square(3) square(3) An environment is a sequence of frames.

• The global frame alone OR


func square(x) 3 func square(x) 3
• A local frame, then the global frame
http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
21 http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
22

Names Have No Meaning Without Environments

1
Every expression is
evaluated in the context
of an environment.

A name evaluates to the


value bound to that name An environment is a sequence of frames.
in the earliest frame of
the current environment in • The global frame alone OR
which that name is found.
(Demo) • A local frame, then the global frame
http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
23

You might also like