本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。
目录
01-1 Functions as Argument Values
01-2 Functions as Return Values
02 Environments of Higher-Order Functions
01 Higher-Order Functions
A function that takes a function as an argument value or returns a function as a return value.
① Express general methods of computation
② Remove repetition(重复) from programs
③ Separate concerns among functions
01-1 Functions as Argument Values

"""Generalization."""
from math import pi, sqrt
def area_square(r):
return r * r
def area_circle(r):
return r * r * pi
def area_hexagon(r):
return r * r * 3 * sqrt(3) / 2
"""Generalize Formal Paraments."""
from math import pi, sqrt
def area(r, shape_constant):
assert r > 0, 'A length must be positive'
return r * r * shape_constant ※
def area_square(r):
return area(r, 1)
def area_circle(r):
return area(r, pi)
def area_hexagon(r):
return area(r, 3 * sqrt(3) / 2)

"""Generalize Functions"""
def natural(k):
return k
def cube(k):
return pow(k, 3)
def pi_term(k):
return 8 / mul(4 * k - 3, a * k - 1)
def summation(n, term):
"""Sum the first n terms of a sequence.
>>> summation(5, cube)
225
"""
total, k = 0, 1
while k <= n:
total, k = total + term(k), k + 1
return total
def sum_naturals(n):
total, k = 0, 1
while k <= n:
total, k = total + k, k + 1
return total
def sum_cubes(n):
total, k = 0, 1
while k <= n:
total, k = total + pow(k, 3), K + 1
return total
python3 -m doctest ex.py
python3 -m doctest -i ex.py
python3 -m doctest -v ex.py
01-2 Functions as Return Values
Functions defined within other function bodies are bound to names in a local frame.
"""Generalization"""
def make_adder(n):
"""Return a function that takes one apartment K and return K + N
>>> adder = make_adder(3)
>>> adder(4)
7
"""
def adder(k):
return k + n
return adder


>>> make_adder(1)(2)
3
>>> make_adder(2000)(25)
2025
>>> f = make_adder(2000)
<function make_adder.<locals>.adder at 十六进制地址>
>>> f(25)
2025
02 Environments of Higher-Order Functions
def apply_twice(f,x):
return f(f(x))
def square(x):
return x * x
Applying a user-defined function:
① Create a new frame
② Bind formal parameters (f & x) to arguments
③ Execute the body: return f(f(x))

def make_adder(n):
def adder(k):
return n + k
return adder
add_three = make_adder(3)
result = add_three(4)

An environment is a sequence of frames.
The environment created by calling a top-level function (no def within def) consists of one local frame, followed by the global frame.
def square(x):
return x * x
def triple(x):
return 3 * x
def composel(f, g):
def h(x):
return f(g(x))
return h
def make_adder(n):
def adder(k):
return n + k
return adder
>>> square(5)
25
>>> triple(5)
15
>>> squiple = composel(square, triple)
>>> squiple(5)
225
>>> composel(square, make_adder(2))(3)
25

03 Lambda Expressions

Lambda expressions are not common in Python, but important in general.
Lambda expressions in Python cannot contain statements at all.

Only the def statement gives the function an intrinsic name.
>>> square = lambda x: x * x
>>> square
<function <lambda> at 十六进制地址>
>>> def square(x):
return x * x
>>> square
<function square at 十六进制地址>
def curry2(f):
def g(x):
def h(y):
return f(x, y)
return h
return g
Currying: Transforming a multi-argument function into a single-argument, higher-order function.
>>> from operator import add
>>> m = curry2(add)
>>> m(2)(3)
5
>>> curry2 = lambda f: lambda x: lambda y: f(x, y)
>>> m = curry2(add)
>>> m(2)(3)
5
A lambda function's parent is the current frame in which the lambda expression is evaluated.

04 What Would Python Print
The print function returns None and it also displays its arguments (separated by spaces) when it is called.

A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found.

def print_all(x):
print(x)
return print_all
print_all(1)(3)(5)
def print_sums(x):
print(x)
def next_sum(y):
return print_sums(x + y)
return next_sum
print_sums(1)(3)(5)
附:词汇解释
execute / ˈeksɪkjuːt / 执行指令(或程序)、interpreter / ɪnˈtɜːrprətər / 解释程序、clause / klɔːz / 子句、suite / swiːt / (程序)组,套、nested 嵌套的、composition 构成、intrinsic 内在的,固有的
compound / ˈkɑːmpaʊnd / statements:复合语句,在编程中,由多个简单语句组成的语句块,通常用于控制流程和逻辑的组织



337

被折叠的 条评论
为什么被折叠?



