Berkeley CS 61A Lecture 2

夙愿已清 提交于 2020-01-25 03:24:08

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:

  1. Evaluate all expressions to the right of = from left to right.
  2. 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:

  1. Create a function with signature : < name >(< formal parameters >)

  2. Set the body of that function to be everything indented after the first line

  3. 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)

  1. Add a local frame, forming a new environment
  2. Bind the function’s formal parameters to its arguments in that frame
  3. 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

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!