本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
cs:fundamental:cs61a:week_1 [2023/09/26 02:23] – [自定义函数的调用过程] codinghare | cs:fundamental:cs61a:week_1 [2023/10/27 05:54] (当前版本) – [Conditional statement] codinghare | ||
---|---|---|---|
行 185: | 行 185: | ||
* 基于 //local frame// 的 name,是独立的;比如上例中的 '' | * 基于 //local frame// 的 name,是独立的;比如上例中的 '' | ||
==Local names== | ==Local names== | ||
+ | 函数的实现与其 //formal parameter// 无关。比如下面的两个函数实际上是同一个: | ||
+ | \\ | ||
+ | <code py> | ||
+ | >>> | ||
+ | return mul(x, x) | ||
+ | >>> | ||
+ | return mul(y, y) | ||
+ | </ | ||
+ | 也就是说,// | ||
+ | * 避免了 //formal parameter// 的 name 与 //global frame// 中的 name 同名带来的问题。 | ||
+ | * 确保了 //formal parameter// 是基于函数独立的;避免了多个函数之间因为参数重名而带来的冲突 | ||
+ | 我们将 //local name// 生效的区域(函数体)成为它的 // | ||
+ | ==python 命名的选择== | ||
+ | * lowercase, spearated by underscores | ||
+ | * 函数名通常表示操作的类型(比如 '' | ||
+ | * argument 使用 lowercase, spearated by underscores,一个词的 name 更好 | ||
+ | * argument 的名字同样也应该表意 | ||
+ | * 不推荐使用单字母参数 | ||
+ | ===函数与抽象=== | ||
+ | 从之前的例子可以看到,我们在不考虑 '' | ||
+ | <code py> | ||
+ | # these two functions are indistinguishable becuase of their return value | ||
+ | >>> | ||
+ | return mul(x, x) | ||
+ | >>> | ||
+ | return mul(x, x-1) + x | ||
+ | </ | ||
+ | ==函数抽象的概念== | ||
+ | 函数抽象需要考虑三个核心的属性: | ||
+ | * The **domain** of a function: 函数可以接受的参数范围 | ||
+ | * The **intent** of function:函数的 input 与 output 之间的关系 | ||
+ | * The **range** of a function: 函数**返回值的取值范围** | ||
+ | ==python 的运算符== | ||
+ | * ''/ | ||
+ | ===Designing Functions=== | ||
+ | 函数设计应该遵循一个 idea: | ||
+ | >// | ||
+ | 具体的来说: | ||
+ | * 每个函数应该只对应一项工作,该工作可以很简单的描述。多个工作应该使用多个函数实现 | ||
+ | * DRY(//do not repeat yourself// | ||
+ | * 函数应该被定义为更泛化的形式。比如比起 '' | ||
+ | ==Documentation== | ||
+ | python 中通常包括了函数的描述,这类 documentation 被称为 // | ||
+ | * // | ||
+ | * // | ||
+ | <code py> | ||
+ | >>> | ||
+ | """ | ||
+ | |||
+ | Applies the ideal gas law: http:// | ||
+ | |||
+ | v -- volume of gas, in cubic meters | ||
+ | t -- absolute temperature in degrees kelvin | ||
+ | n -- particles of gas | ||
+ | """ | ||
+ | k = 1.38e-23 | ||
+ | return n * k * t / v | ||
+ | </ | ||
+ | 还有一类以 ''#'' | ||
+ | [[https:// | ||
+ | ====Control statement==== | ||
+ | ===statement=== | ||
+ | // | ||
+ | * 我们评估(// | ||
+ | * 我们执行(// | ||
+ | // | ||
+ | <code py> | ||
+ | >>> | ||
+ | mul(x, x) # Watch out! This call doesn' | ||
+ | </ | ||
+ | 如果希望应用修改(返回返回值),则需要使用 '' | ||
+ | <code py> | ||
+ | >>> | ||
+ | return mul(x, x) | ||
+ | </ | ||
+ | ==Compound Statements== | ||
+ | 结构如下: | ||
+ | <code py> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | ... | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | ... | ||
+ | ... | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | 根据上述定义,'' | ||
+ | </ | ||
+ | 上述的结构可以被视作 // | ||
+ | * 当前的 statement | ||
+ | * 余下的 statement | ||
+ | 这是一种递归的结构。 | ||
+ | ===Local Assignment=== | ||
+ | 用户自定义的函数是在对应的 local frame 中运行的。local frame 在该函数被调用时创建;函数体中的 '' | ||
+ | * 第一个 '' | ||
+ | * 返回的值 | ||
+ | 赋值语句可以处于函数内部。任何函数内部的赋值,其绑定信息都存储于local frame,对外部的 name 不造成任何影响。 | ||
+ | ===Conditional statement=== | ||
+ | <code py> | ||
+ | if < | ||
+ | < | ||
+ | elif < | ||
+ | < | ||
+ | else: | ||
+ | < | ||
+ | </ | ||
+ | * 首先会评估 header | ||
+ | * 某个 header 为 true, 则执行该 Header 下的 suite,其他的 suite 将会被跳过。 | ||
+ | ===Iteration=== | ||
+ | <code py> | ||
+ | while < | ||
+ | < | ||
+ | </ | ||
+ | ===Testing=== | ||
+ | ==Assertions== | ||
+ | python 中可以使用 '' | ||
+ | <code py> | ||
+ | >>> | ||
+ | assert fib(2) == 1, 'The 2nd Fibonacci number should be 1' | ||
+ | assert fib(3) == 1, 'The 3rd Fibonacci number should be 1' | ||
+ | assert fib(50) == 7778742049, 'Error at the 50th Fibonacci number' | ||
+ | </ | ||
+ | ==Doctest== | ||
+ | // | ||
+ | <code py> | ||
+ | >>> | ||
+ | """ | ||
+ | |||
+ | >>> | ||
+ | 55 | ||
+ | >>> | ||
+ | 5050 | ||
+ | """ | ||
+ | total, k = 0, 1 | ||
+ | while k <= n: | ||
+ | total, k = total + k, k + 1 | ||
+ | return total | ||
+ | | ||
+ | >>> | ||
+ | >>> | ||
+ | TestResults(failed=0, | ||
+ | </ |