http://inst.eecs.berkeley.edu/~cs61a/sp18/
CS 61A LECTURE 2
Names, Assignment , and User-Defined Function
>>> f = max
>>> f
<built-in function max>
>>> max
<built-in function max>
>>> f(1,2,3)
3
>>> max = 7
>>> f(1,2,3)
3
>>> from operator import add, mul
>>> def square(x):
... return mul(x, x)
...
>>> square
<function square at 0x000001FA67B15400>
>>> square(11)
121
>>> square(add(3, 4))
49
>>> square(square(3))
81
>>> def sum_squares(x, y):
... return square(x) + square(y)
...
>>> sum_squares(3,4)
25
>>> radius = 20
>>> from math import pi
>>> area = pi
>>> radius
20
>>> area
3.141592653589793
>>> def area():
... return pi * radius * radius
...
>>> area
<function area at 0x000001FA67B15378>
>>> area()
1256.6370614359173
>>> pi * 20 * 20
1256.6370614359173
>>> radius = 10
>>> area()
314.1592653589793
>>> radius = 1
>>> area()
3.141592653589793
Types of Expressions
Primitive expressions:
Call expressions:
Discussion Question 1
What is the value of the final expression in this sequence?
>>> f = min
>>> f = max
>>> g, h = min, max
>>> max = g
>>> max(f(2,g(h(1,5),3)),4)
ans:
3
>>> max
<built-in function min>
>>> h
<built-in function max>
>>> g
<built-in function min>
Environment Diagrams
http://pythontutor.com/composingprograms.html#mode=display
Environment diagrams visualize the interpreter’s process
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 |
Assignment Statements
Execution rule for assignment statements:
- Evaluate all expressions to the right of = from left to right.
- Bind all names to the left of = to the resulting values in the current frame.
Discussion Question 1 Solution
Defining Functions
Assignment(赋值) is a simple means of abstraction: binds names to values
Function definition(函数定义) is a more powerful means of abstraction: binds names to expressions
Execution procedure for def statements:
-
Create a function with signature : < name >(< formal parameters >)
-
Set the body of that function to be everything indented after the first line
-
Bind < name > to that function in the current frame
DEF Not executed until the function is called
Calling User-Defined Function
Procedure for calling/applying user-defined functions (version 1)
- Add a local frame, forming a new environment
- Bind the function’s formal parameters to its arguments in that frame
- Execute the body of the function in that new environment
A function’s signature has all the information needed to create a local frame
Looking Up Names In Environments
Every expression is evaluated in the context of an environment.
So far, the current environment is either :
- The global frame alone, or
- A local frame, followed by the global frame.
Most important two things I’ll say all day:
An environment is a sequence of frames.
A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found.
E.g., to look up some name in the body of the square function:
-
Look for that name in local frame.
-
If not found, look for it in global frame.
(Built-in names like “max” are in the global frame too, but we don’t draw them in environment diagrams.)
>>> from operator import mul
>>> mul(3, 4)
12
>>> def square(square):
... return mul(square, square)
...
>>> square
<function square at 0x000001CA56EBC1E0>
>>> square(4)
16