4. 更多控制流工具
除了刚介绍的 while 语句,Python 还用了一些别的。我们将在本章中遇到它们。4.1. if 语句
最让人耳熟能详的语句应当是 if 语句:
>>>
可有零个或多个 elif 部分,else 部分也是可选的。关键字 ‘
>>> x = int(input("Please enter an integer: "))Please enter an integer: 42>>> if x < 0:... x = 0... print('Negative changed to zero')... elif x == 0:... print('Zero')... elif x == 1:... print('Single')... else:... print('More')...More
<font style="color:rgb(0, 0, 0);">elif</font>‘ 是 ‘else if’ 的缩写,用于避免过多的缩进。<font style="color:rgb(0, 0, 0);">if</font> … <font style="color:rgb(0, 0, 0);">elif</font> … <font style="color:rgb(0, 0, 0);">elif</font> … 序列可以当作其它语言中 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">switch</font> 或 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">case</font> 语句的替代品。
如果是把一个值与多个常量进行比较,或者检查特定类型或属性,<font style="color:rgb(0, 0, 0);">match</font> 语句更有用。详见 match 语句。
4.2. for 语句
Python 的 for 语句与 C 或 Pascal 中的不同。Python 的 <font style="color:rgb(0, 0, 0);">for</font> 语句不迭代算术递增数值(如 Pascal),或是给予用户定义迭代步骤和结束条件的能力(如 C),而是在列表或字符串等任意序列的元素上迭代,按它们在序列中出现的顺序。 例如(这不是有意要暗指什么):
>>>
很难正确地在迭代多项集的同时修改多项集的内容。更简单的方法是迭代多项集的副本或者创建新的多项集:
>>> # Measure some strings:... words = ['cat', 'window', 'defenestrate']>>> for w in words:... print(w, len(w))...cat 3window 6defenestrate 12
# Create a sample collectionusers = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}# Strategy: Iterate over a copyfor user, status in users.copy().items():if status == 'inactive':del users[user]# Strategy: Create a new collectionactive_users = {}for user, status in users.items():if status == 'active':active_users[user] = status
4.3. range() 函数
内置函数 range() 用于生成等差数列: >>>生成的序列绝不会包括给定的终止值;
>>> for i in range(5):... print(i)...01234
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">range(10)</font> 生成 10 个值——长度为 10 的序列的所有合法索引。range 可以不从 0 开始,且可以按给定的步长递增(即使是负数步长):
>>>
要按索引迭代序列,可以组合使用 range() 和 len(): >>>
>>> list(range(5, 10))[5, 6, 7, 8, 9]>>> list(range(0, 10, 3))[0, 3, 6, 9]>>> list(range(-10, -100, -30))[-10, -40, -70]
不过大多数情况下 enumerate() 函数很方便,详见 循环的技巧。 如果直接打印一个 range 会发生意想不到的事情: >>>
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']>>> for i in range(len(a)):... print(i, a[i])...0 Mary1 had2 a3 little4 lamb
>>> range(10)range(0, 10)
range() 返回的对象在很多方面和列表的行为一样,但其实它和列表不一样。该对象只有在被迭代时才一个一个地返回所期望的列表项,并没有真正生成过一个含有全部项的列表,从而节省了空间。
这种对象称为可迭代对象 iterable,适合作为需要获取一系列值的函数或程序构件的参数。for 语句就是这样的程序构件;以可迭代对象作为参数的函数例如 sum(): >>>之后我们会看到更多返回可迭代对象,或以可迭代对象作为参数的函数。在 数据结构 这一章中,我们将讨论 list() 的更多细节。
>>> sum(range(4)) # 0 + 1 + 2 + 36
4.4. 循环中的 break、continue 语句及 else 子句
break 语句将跳出最近的一层 for 或 while 循环。
<font style="color:rgb(0, 0, 0);">for</font> 或 <font style="color:rgb(0, 0, 0);">while</font> 循环可以包括 <font style="color:rgb(0, 0, 0);">else</font> 子句。
<font style="color:rgb(0, 0, 0);">else</font> 子句会在循环成功结束最后一次迭代之后执行。
在 while 循环中,它会在循环条件变为假值后执行。
无论哪种循环,如果因为 break 而结束,那么 <font style="color:rgb(0, 0, 0);">else</font> 子句就 不会 执行。
下面的搜索质数的 <font style="color:rgb(0, 0, 0);">for</font> 循环就是一个例子:
>>>
(没错,这段代码就是这么写。仔细看:
>>> for n in range(2, 10):... for x in range(2, n):... if n % x == 0:... print(n, 'equals', x, '*', n//x)... break... else:... # loop fell through without finding a factor... print(n, 'is a prime number')...2 is a prime number3 is a prime number4 equals 2 * 25 is a prime number6 equals 2 * 37 is a prime number8 equals 2 * 49 equals 3 * 3
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">else</font> 子句属于 for 循环,不属于 if 语句。)
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">else</font> 子句用于循环时比起 if 语句的 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">else</font> 子句,更像 try 语句的。try 语句的 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">else</font> 子句在未发生异常时执行,循环的 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">else</font> 子句则在未发生 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">break</font> 时执行。 <font style="color:rgb(0, 0, 0);">try</font> 语句和异常详见 异常的处理。
continue 语句,同样借鉴自 C 语言,以执行循环的下一次迭代来继续:
>>>
>>> for num in range(2, 10):... if num % 2 == 0:... print("Found an even number", num)... continue... print("Found an odd number", num)...Found an even number 2Found an odd number 3Found an even number 4Found an odd number 5Found an even number 6Found an odd number 7Found an even number 8Found an odd number 9
4.5. pass 语句
pass 语句不执行任何动作。语法上需要一个语句,但程序毋需执行任何动作时,可以使用该语句。例如:
>>>这常用于创建一个最小的类: >>>
>>> while True:... pass # Busy-wait for keyboard interrupt (Ctrl+C)...
>>> class MyEmptyClass:... pass...
pass 还可用作函数或条件语句体的占位符,让你保持在更抽象的层次进行思考。<font style="color:rgb(0, 0, 0);">pass</font> 会被默默地忽略:
>>> def initlog(*args):... pass # Remember to implement this!...
4.6. match 语句
match 语句接受一个表达式并把它的值与一个或多个 case 块给出的一系列模式进行比较。这表面上像 C、Java 或 JavaScript(以及许多其他程序设计语言)中的 switch 语句,但其实它更像 Rust 或 Haskell 中的模式匹配。只有第一个匹配的模式会被执行,并且它还可以提取值的组成部分(序列的元素或对象的属性)赋给变量。
最简单的形式是将一个主语值与一个或多个字面值进行比较:注意最后一个代码块:“变量名”
def http_error(status):match status:case 400:return "Bad request"case 404:return "Not found"case 418:return "I'm a teapot"case _:return "Something's wrong with the internet"
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">_</font> 被作为 通配符 并必定会匹配成功。如果没有 case 匹配成功,则不会执行任何分支。
你可以使用 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">|</font> (“ or ”)在一个模式中组合几个字面值:
形如解包赋值的模式可被用于绑定变量:
case 401 | 403 | 404:return "Not allowed"
请仔细学习此代码!第一个模式有两个字面值,可视为前述字面值模式的扩展。接下来的两个模式结合了一个字面值和一个变量,变量 绑定 了来自主语(
# point is an (x, y) tuplematch point:case (0, 0):print("Origin")case (0, y):print(f"Y={y}")case (x, 0):print(f"X={x}")case (x, y):print(f"X={x}, Y={y}")case _:raise ValueError("Not a point")
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">point</font>)的一个值。第四个模式捕获了两个值,使其在概念上与解包赋值 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">(x,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y)</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">point</font> 类似。
如果用类组织数据,可以用“类名后接一个参数列表”这种很像构造器的形式,把属性捕获到变量里:
你可以在某些为其属性提供了排序的内置类(例如 dataclass)中使用位置参数。 你也可以通过在你的类中设置
class Point:def __init__(self, x, y):self.x = xself.y = ydef where_is(point):match point:case Point(x=0, y=0):print("Origin")case Point(x=0, y=y):print(f"Y={y}")case Point(x=x, y=0):print(f"X={x}")case Point():print("Somewhere else")case _:print("Not a point")
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">__match_args__</font> 特殊属性来为模式中的属性定义一个专门的位置。 如果它被设为 (“x”, “y”),则以下模式均为等价的(并且都是将 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y</font> 属性绑定到 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">var</font> 变量):
建议这样来阅读一个模式——通过将其视为赋值语句等号左边的一种扩展形式,来理解各个变量被设为何值。match 语句只会为单一的名称(如上面的
Point(1, var)Point(1, y=var)Point(x=1, y=var)Point(y=var, x=1)
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">var</font>)赋值,而不会赋值给带点号的名称(如 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">foo.bar</font>)、属性名(如上面的 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">x=</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y=</font>)和类名(是通过其后的 “(…)” 来识别的,如上面的 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">Point</font>)。
模式可以任意嵌套。举例来说,如果我们有一个由 Point 组成的列表,且 Point 添加了 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">__match_args__</font> 时,我们可以这样来匹配它:
我们可以向一个模式添加
class Point:__match_args__ = ('x', 'y')def __init__(self, x, y):self.x = xself.y = ymatch points:case []:print("No points")case [Point(0, 0)]:print("The origin")case [Point(x, y)]:print(f"Single point {x}, {y}")case [Point(0, y1), Point(0, y2)]:print(f"Two on the Y axis at {y1}, {y2}")case _:print("Something else")
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">if</font> 子句,称为“约束项”。 如果约束项为假值,则 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">match</font> 将继续尝试下一个 case 语句块。 请注意值的捕获发生在约束项被求值之前。:
该语句的一些其它关键特性:
match point:case Point(x, y) if x == y:print(f"Y=X at {x}")case Point(x, y):print(f"Not on the diagonal")
- 与解包赋值类似,元组和列表模式具有完全相同的含义并且实际上都能匹配任意序列,区别是它们不能匹配迭代器或字符串。
- 序列模式支持扩展解包:
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">[x,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*rest]</font>和<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">(x,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*rest)</font>和相应的解包赋值做的事是一样的。接在<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font>后的名称也可以为<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">_</font>,所以<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">(x,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*_)</font>匹配含至少两项的序列,而不必绑定剩余的项。 - 映射模式:
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">{"bandwidth":</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">b,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">"latency":</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">l}</font>从字典中捕获<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">"bandwidth"</font>和<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">"latency"</font>的值。额外的键会被忽略,这一点与序列模式不同。<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**rest</font>这样的解包也支持。(但<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**_</font>将会是冗余的,故不允许使用。) - 子模式可使用
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">as</font>关键字来捕获:
将把输入中的第二个元素捕获为
case (Point(x1, y1), Point(x2, y2) as p2): ...
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">p2</font> (只要输入是包含两个点的序列)
- 大多数字面值是按相等性比较的,但是单例对象
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">True</font>、<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">False</font>和<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font>则是按 id 比较的。 - 模式可以使用具名常量。它们必须作为带点号的名称出现,以防止它们被解释为用于捕获的变量:
更详细的说明和更多示例,可参阅以教程格式撰写的 PEP 636。
from enum import Enumclass Color(Enum):RED = 'red'GREEN = 'green'BLUE = 'blue'color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))match color:case Color.RED:print("I see red!")case Color.GREEN:print("Grass is green")case Color.BLUE:print("I'm feeling the blues :(")
4.7. 定义函数
下列代码创建一个可以输出限定数值内的斐波那契数列函数: >>>
>>> def fib(n): # write Fibonacci series up to n... """Print a Fibonacci series up to n."""... a, b = 0, 1... while a < n:... print(a, end=' ')... a, b = b, a+b... print()...>>> # Now call the function we just defined:... fib(2000)0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
定义 函数使用关键字 def,后跟函数名与括号内的形参列表。函数语句从下一行开始,并且必须缩进。
函数内的第一条语句是字符串时,该字符串就是文档字符串,也称为 docstring,详见 文档字符串。利用文档字符串可以自动生成在线文档或打印版文档,还可以让开发者在浏览代码时直接查阅文档;Python 开发者最好养成在代码中加入文档字符串的好习惯。 函数在 执行 时使用函数局部变量符号表,所有函数变量赋值都存在局部符号表中;引用变量时,首先,在局部符号表里查找变量,然后,是外层函数局部符号表,再是全局符号表,最后是内置名称符号表。因此,尽管可以引用全局变量和外层函数的变量,但最好不要在函数内直接赋值(除非是 global 语句定义的全局变量,或 nonlocal 语句定义的外层函数变量)。 在调用函数时会将实际参数(实参)引入到被调用函数的局部符号表中;因此,实参是使用 按值调用 来传递的(其中的 值 始终是对象的 引用 而不是对象的值)。 [1] 当一个函数调用另外一个函数时,会为该调用创建一个新的局部符号表。 函数定义在当前符号表中把函数名与函数对象关联在一起。解释器把函数名指向的对象作为用户自定义函数。还可以使用其他名称指向同一个函数对象,并访问访该函数: >>>如果你用过其他语言,你可能会认为
>>> fib<function fib at 10042ed0>>>> f = fib>>> f(100)0 1 1 2 3 5 8 13 21 34 55 89
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">fib</font> 不是函数而是一个过程,因为它没有返回值。 事实上,即使没有 return 语句的函数也有返回值,尽管这个值可能相当无聊。 这个值被称为 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font> (是一个内置名称)。 通常解释器会屏蔽单独的返回值 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font>。 如果你确有需要可以使用 print() 查看它:
>>>
编写不直接输出斐波那契数列运算结果,而是返回运算结果列表的函数也非常简单: >>>
>>> fib(0)>>> print(fib(0))None
本例也新引入了一些 Python 功能:
>>> def fib2(n): # return Fibonacci series up to n... """Return a list containing the Fibonacci series up to n."""... result = []... a, b = 0, 1... while a < n:... result.append(a) # see below... a, b = b, a+b... return result...>>> f100 = fib2(100) # call it>>> f100 # write the result[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- return 语句返回函数的值。
<font style="color:rgb(0, 0, 0);">return</font>语句不带表达式参数时,返回<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font>。函数执行完毕退出也返回<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font>。 - 语句
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">result.append(a)</font>调用了列表对象<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">result</font>的 方法。 方法是‘从属于’对象的函数,其名称为<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">obj.methodname</font>,其中<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">obj</font>是某个对象(可以是一个表达式),<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">methodname</font>是由对象的类型定义的方法名称。 不同的类型定义了不同的方法。 不同的类型的方法可以使用相同的名称而不会产生歧义。 (使用 类 可以定义自己的对象类型和方法,参见 类。) 在示例中显示的方法<font style="color:rgb(0, 0, 0);">append()</font>是由列表对象定义的;它会在列表的末尾添加一个新元素。 在本例中它等同于<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">result</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">result</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">+</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">[a]</font>,但效率更高。
4.8. 函数定义详解
函数定义支持可变数量的参数。这里列出三种可以组合使用的形式。4.8.1. 默认值参数
为参数指定默认值是非常有用的方式。调用函数时,可以使用比定义时更少的参数,例如:该函数可以用以下方式调用:
def ask_ok(prompt, retries=4, reminder='Please try again!'):while True:reply = input(prompt)if reply in {'y', 'ye', 'yes'}:return Trueif reply in {'n', 'no', 'nop', 'nope'}:return Falseretries = retries - 1if retries < 0:raise ValueError('invalid user response')print(reminder)
- 只给出必选实参:
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">ask_ok('Do</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">you</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">really</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">want</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">to</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">quit?')</font> - 给出一个可选实参:
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">ask_ok('OK</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">to</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">overwrite</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">the</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">file?',</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">2)</font> - 给出所有实参:
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">ask_ok('OK</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">to</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">overwrite</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">the</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">file?',</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">2,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">'Come</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">on,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">only</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">yes</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">or</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">no!')</font>
上例输出的是
i = 5def f(arg=i):print(arg)i = 6f()
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">5</font>。
重要警告: 默认值只计算一次。默认值为列表、字典或类实例等可变对象时,会产生与该规则不同的结果。例如,下面的函数会累积后续调用时传递的参数:
输出结果如下:
def f(a, L=[]):L.append(a)return Lprint(f(1))print(f(2))print(f(3))
不想在后续调用之间共享默认值时,应以如下方式编写函数:
[1][1, 2][1, 2, 3]
def f(a, L=None):if L is None:L = []L.append(a)return L
4.8.2. 关键字参数
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">kwarg=value</font> 形式的 关键字参数 也可以用于调用函数。函数示例如下:
该函数接受一个必选参数(
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):print("-- This parrot wouldn't", action, end=' ')print("if you put", voltage, "volts through it.")print("-- Lovely plumage, the", type)print("-- It's", state, "!")
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">voltage</font>)和三个可选参数(<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">state</font>, <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">action</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">type</font>)。该函数可用下列方式调用:
以下调用函数的方式都无效:
parrot(1000) # 1 positional argumentparrot(voltage=1000) # 1 keyword argumentparrot(voltage=1000000, action='VOOOOOM') # 2 keyword argumentsparrot(action='VOOOOOM', voltage=1000000) # 2 keyword argumentsparrot('a million', 'bereft of life', 'jump') # 3 positional argumentsparrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
函数调用时,关键字参数必须跟在位置参数后面。所有传递的关键字参数都必须匹配一个函数接受的参数(比如,
parrot() # required argument missingparrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argumentparrot(110, voltage=220) # duplicate value for the same argumentparrot(actor='John Cleese') # unknown keyword argument
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">actor</font> 不是函数 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">parrot</font> 的有效参数),关键字参数的顺序并不重要。这也包括必选参数,(比如,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">parrot(voltage=1000)</font> 也有效)。不能对同一个参数多次赋值,下面就是一个因此限制而失败的例子:
>>>
最后一个形参为
>>> def function(a):... pass...>>> function(0, a=0)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: function() got multiple values for argument 'a'
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**name</font> 形式时,接收一个字典(详见 映射类型 —- dict),该字典包含与函数中已定义形参对应之外的所有关键字参数。<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**name</font> 形参可以与 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*name</font> 形参(下一小节介绍)组合使用(<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*name</font> 必须在 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**name</font> 前面), <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*name</font> 形参接收一个 元组,该元组包含形参列表之外的位置参数。例如,可以定义下面这样的函数:
该函数可以用如下方式调用:
def cheeseshop(kind, *arguments, **keywords):print("-- Do you have any", kind, "?")print("-- I'm sorry, we're all out of", kind)for arg in arguments:print(arg)print("-" * 40)for kw in keywords:print(kw, ":", keywords[kw])
输出结果如下:
cheeseshop("Limburger", "It's very runny, sir.","It's really very, VERY runny, sir.",shopkeeper="Michael Palin",client="John Cleese",sketch="Cheese Shop Sketch")
注意,关键字参数在输出结果中的顺序与调用函数时的顺序一致。
-- Do you have any Limburger ?-- I'm sorry, we're all out of LimburgerIt's very runny, sir.It's really very, VERY runny, sir.----------------------------------------shopkeeper : Michael Palinclient : John Cleesesketch : Cheese Shop Sketch
4.8.3. 特殊参数
默认情况下,参数可以按位置或显式关键字传递给 Python 函数。为了让代码易读、高效,最好限制参数的传递方式,这样,开发者只需查看函数定义,即可确定参数项是仅按位置、按位置或关键字,还是仅按关键字传递。 函数定义如下:
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):----------- ---------- ----------| | || Positional or keyword || - Keyword only-- Positional only
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font> 是可选的。这些符号表明形参如何把参数值传递给函数:位置、位置或关键字、关键字。关键字形参也叫作命名形参。
4.8.3.1. 位置或关键字参数
函数定义中未使用<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font> 时,参数可以按位置或关键字传递给函数。
4.8.3.2. 仅位置参数
此处再介绍一些细节,特定形参可以标记为 仅限位置。仅限位置 时,形参的顺序很重要,且这些形参不能用关键字传递。仅限位置形参应放在<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> (正斜杠)前。<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> 用于在逻辑上分割仅限位置形参与其它形参。如果函数定义中没有 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font>,则表示没有仅限位置形参。
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> 后可以是 位置或关键字 或 仅限关键字 形参。
4.8.3.3. 仅限关键字参数
把形参标记为 仅限关键字,表明必须以关键字参数形式传递该形参,应在参数列表中第一个 仅限关键字 形参前添加<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font>。
4.8.3.4. 函数示例
请看下面的函数定义示例,注意<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font> 标记:
>>>
第一个函数定义
>>> def standard_arg(arg):... print(arg)...>>> def pos_only_arg(arg, /):... print(arg)...>>> def kwd_only_arg(*, arg):... print(arg)...>>> def combined_example(pos_only, /, standard, *, kwd_only):... print(pos_only, standard, kwd_only)
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">standard_arg</font> 是最常见的形式,对调用方式没有任何限制,可以按位置也可以按关键字传递参数:
>>>
第二个函数
>>> standard_arg(2)2>>> standard_arg(arg=2)2
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">pos_only_arg</font> 的函数定义中有 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font>,仅限使用位置形参:
>>>
第三个函数
>>> pos_only_arg(1)1>>> pos_only_arg(arg=1)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: pos_only_arg() got some positional-only arguments passed as keyword arguments: 'arg'
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">kwd_only_args</font> 的函数定义通过 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font> 表明仅限关键字参数:
>>>
最后一个函数在同一个函数定义中,使用了全部三种调用惯例: >>>
>>> kwd_only_arg(3)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: kwd_only_arg() takes 0 positional arguments but 1 was given>>> kwd_only_arg(arg=3)3
下面的函数定义中,
>>> combined_example(1, 2, 3)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: combined_example() takes 2 positional arguments but 3 were given>>> combined_example(1, 2, kwd_only=3)1 2 3>>> combined_example(1, standard=2, kwd_only=3)1 2 3>>> combined_example(pos_only=1, standard=2, kwd_only=3)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: combined_example() got some positional-only arguments passed as keyword arguments: 'pos_only'
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">kwds</font> 把 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">name</font> 当作键,因此,可能与位置参数 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">name</font> 产生潜在冲突:
调用该函数不可能返回
def foo(name, **kwds):return 'name' in kwds
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">True</font>,因为关键字 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">'name'</font> 总与第一个形参绑定。例如:
>>>
加上
>>> foo(1, **{'name': 2})Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: foo() got multiple values for argument 'name'>>>
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">/</font> (仅限位置参数)后,就可以了。此时,函数定义把 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">name</font> 当作位置参数,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">'name'</font> 也可以作为关键字参数的键:
>>>
换句话说,仅限位置形参的名称可以在
>>> def foo(name, /, **kwds):... return 'name' in kwds...>>> foo(1, **{'name': 2})True
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**kwds</font> 中使用,而不产生歧义。
4.8.3.5. 小结
以下用例决定哪些形参可以用于函数定义:说明:
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
- 使用仅限位置形参,可以让用户无法使用形参名。形参名没有实际意义时,强制调用函数的实参顺序时,或同时接收位置形参和关键字时,这种方式很有用。
- 当形参名有实际意义,且显式名称可以让函数定义更易理解时,阻止用户依赖传递实参的位置时,才使用关键字。
- 对于 API,使用仅限位置形参,可以防止未来修改形参名时造成破坏性的 API 变动。
4.8.4. 任意实参列表
调用函数时,使用任意数量的实参是最少见的选项。这些实参包含在元组中(详见 元组和序列 )。在可变数量的实参之前,可能有若干个普通参数:
def write_multiple_items(file, separator, *args):file.write(separator.join(args))
variadic 参数用于采集传递给函数的所有剩余参数,因此,它们通常在形参列表的末尾。<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*args</font> 形参后的任何形式参数只能是仅限关键字参数,即只能用作关键字参数,不能用作位置参数:
>>> def concat(*args, sep="/"):... return sep.join(args)...>>> concat("earth", "mars", "venus")'earth/mars/venus'>>> concat("earth", "mars", "venus", sep=".")'earth.mars.venus'
4.8.5. 解包实参列表
函数调用要求独立的位置参数,但实参在列表或元组里时,要执行相反的操作。例如,内置的 range() 函数要求独立的 start 和 stop 实参。如果这些参数不是独立的,则要在调用函数时,用<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">*</font> 操作符把实参从列表或元组解包出来:
>>>
同样,字典可以用
>>> list(range(3, 6)) # normal call with separate arguments[3, 4, 5]>>> args = [3, 6]>>> list(range(*args)) # call with arguments unpacked from a list[3, 4, 5]
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">**</font> 操作符传递关键字参数:
>>>
>>> def parrot(voltage, state='a stiff', action='voom'):... print("-- This parrot wouldn't", action, end=' ')... print("if you put", voltage, "volts through it.", end=' ')... print("E's", state, "!")...>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}>>> parrot(**d)-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
4.8.6. Lambda 表达式
lambda 关键字用于创建小巧的匿名函数。<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">lambda</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">b:</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a+b</font> 函数返回两个参数的和。Lambda 函数可用于任何需要函数对象的地方。在语法上,匿名函数只能是单个表达式。在语义上,它只是常规函数定义的语法糖。与嵌套函数定义一样,lambda 函数可以引用包含作用域中的变量:
上例用 lambda 表达式返回函数。还可以把匿名函数用作传递的实参: >>>
>>> def make_incrementor(n):... return lambda x: x + n...>>> f = make_incrementor(42)>>> f(0)42>>> f(1)43
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]>>> pairs.sort(key=lambda pair: pair[1])>>> pairs[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
4.8.7. 文档字符串
以下是文档字符串内容和格式的约定。 第一行应为对象用途的简短摘要。为保持简洁,不要在这里显式说明对象名或类型,因为可通过其他方式获取这些信息(除非该名称碰巧是描述函数操作的动词)。这一行应以大写字母开头,以句点结尾。 文档字符串为多行时,第二行应为空白行,在视觉上将摘要与其余描述分开。后面的行可包含若干段落,描述对象的调用约定、副作用等。 Python 解析器不会删除 Python 中多行字符串字面值的缩进,因此,文档处理工具应在必要时删除缩进。这项操作遵循以下约定:文档字符串第一行 之后 的第一个非空行决定了整个文档字符串的缩进量(第一行通常与字符串开头的引号相邻,其缩进在字符串中并不明显,因此,不能用第一行的缩进),然后,删除字符串中所有行开头处与此缩进“等价”的空白符。不能有比此缩进更少的行,但如果出现了缩进更少的行,应删除这些行的所有前导空白符。转化制表符后(通常为 8 个空格),应测试空白符的等效性。 下面是多行文档字符串的一个例子: >>>
>>> def my_function():... """Do nothing, but document it....... No, really, it doesn't do anything.... """... pass...>>> print(my_function.__doc__)Do nothing, but document it.No, really, it doesn't do anything.
4.8.8. 函数注解
函数注解 是可选的用户自定义函数类型的元数据完整信息(详见 PEP 3107 和 PEP 484 )。
标注 以字典的形式存放在函数的 <font style="color:rgb(0, 0, 0);">__annotations__</font> 属性中而对函数的其他部分没有影响。 形参标注的定义方式是在形参名后加冒号,后面跟一个会被求值为标注的值的表达式。 返回值标注的定义方式是加组合符号 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">-></font>,后面跟一个表达式,这样的校注位于形参列表和表示 def 语句结束的冒号。 下面的示例有一个必须的参数、一个可选的关键字参数以及返回值都带有相应的标注:
>>> def f(ham: str, eggs: str = 'eggs') -> str:... print("Annotations:", f.__annotations__)... print("Arguments:", ham, eggs)... return ham + ' and ' + eggs...>>> f('spam')Annotations: {'ham': <class 'str'>, 'return': <class 'str'>, 'eggs': <class 'str'>}Arguments: spam eggs'spam and eggs'
4.9. 小插曲:编码风格
现在你将要写更长,更复杂的 Python 代码,是时候讨论一下 代码风格 了。 大多数语言都能以不同的风格被编写(或更准确地说,被格式化);有些比其他的更具有可读性。 能让其他人轻松阅读你的代码总是一个好主意,采用一种好的编码风格对此有很大帮助。 Python 项目大多都遵循 PEP 8 的风格指南;它推行的编码风格易于阅读、赏心悦目。Python 开发者均应抽时间悉心研读;以下是该提案中的核心要点:- 缩进,用 4 个空格,不要用制表符。
- 换行,一行不超过 79 个字符。
- 用空行分隔函数和类,及函数内较大的代码块。
- 最好把注释放到单独一行。
- 使用文档字符串。
- 运算符前后、逗号后要用空格,但不要直接在括号内使用:
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">f(1,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">2)</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">+</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">g(3,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">4)</font>。 - 类和函数的命名要一致;按惯例,命名类用
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">UpperCamelCase</font>,命名函数与方法用<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">lowercase_with_underscores</font>。命名方法中第一个参数总是用<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">self</font>(类和方法详见 初探类)。 - 编写用于国际多语环境的代码时,不要用生僻的编码。Python 默认的 UTF-8 或纯 ASCII 可以胜任各种情况。
- 同理,就算多语阅读、维护代码的可能再小,也不要在标识符中使用非 ASCII 字符。
备注
[1] 实际上,对象引用调用 这种说法更好,因为,传递的是可变对象时,调用者能发现被调者做出的任何更改(插入列表的元素)。