Python中的pass是空语句,是为了保持程序结构的完整性。pass不做任何事情,一般用做占位语句。
- for letter in 'Python':
- if letter == 'h':
- pass
- print('这是 pass 块')
- print('当前字母 :', letter)
-
- print("Good bye!")
执行上述代码,其输出内容:
- 当前字母 : P
- 当前字母 : y
- 当前字母 : t
- 这是 pass 块
- 当前字母 : h
- 当前字母 : o
- 当前字母 : n
- Good bye!
在Python中有时候会看到一个 def 函数:
- def method1():
- pass
该处的 pass 便是占据一个位置,因为如果定义一个空函数程序会报错,当你没有想好函数的内容是可以用 pass 填充,使程序可以正常运行。
为了跳过for循环里的某次循环,以下代码当某次循环发生错误时,执行except代码块,continue跳