What & How & Why

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
后一修订版两侧同时换到之后的修订记录
cs:fundamental:cs61a:week_1 [2023/09/26 03:50] codingharecs:fundamental:cs61a:week_1 [2023/09/26 04:13] – [Control] codinghare
行 219: 行 219:
 ==python 的运算符== ==python 的运算符==
   * ''/ /'' 是 floor division, ''/'' 是一般除法,分别对应 ''floordiv()'' 和 ''truediv()''   * ''/ /'' 是 floor division, ''/'' 是一般除法,分别对应 ''floordiv()'' 和 ''truediv()''
-====Designing Functions====+===Designing Functions=== 
 +函数设计应该遵循一个 idea: 
 +>//functions are abstractions.// 
 +具体的来说: 
 +  * 每个函数应该只对应一项工作,该工作可以很简单的描述。多个工作应该使用多个函数实现 
 +  * DRY(//do not repeat yourself//)。逻辑只需要被实现一次,并反复应用;而不是到处拷贝该逻辑 
 +  *  函数应该被定义为更泛化的形式。比如比起 ''square()'' 函数,我们应该实现的是 ''pow()'' 函数。因为指数运算包含了平方运算。 
 +==Documentation== 
 +python 中通常包括了函数的描述,这类 documentation 被称为 //docstring//: 
 +  * //docstring// 使用首尾三对双引号包含 
 +  * //docstring// 需要遵循缩进 
 +<code py> 
 +>>> def pressure(v, t, n): 
 +        """Compute the pressure in pascals of an ideal gas. 
 + 
 +        Applies the ideal gas law: http://en.wikipedia.org/wiki/Ideal_gas_law 
 + 
 +        v -- volume of gas, in cubic meters 
 +        t -- absolute temperature in degrees kelvin 
 +        n -- particles of gas 
 +        """ 
 +        k = 1.38e-23  # Boltzmann's constant 
 +        return n * k * t / v 
 +</code> 
 +还有一类以 ''#'' 起头的信息被称为 //Comments//。 
 +[[https://peps.python.org/pep-0257/|Docstring Conventions]] 
 +====Control====