Berkeley CS 61A Lecture 2

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 expressionsEach name is bound to a value
Arrows indicate evaluation orderWithin 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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值