4. 流程控制语句
4.1. 语句
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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
</pre>
4.2. 语句
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
</pre>
如果要在循环内修改正在迭代的序列(例如,复制所选的项目),建议首先创建原序列的拷贝。对序列进行迭代不会隐式地进行复制。切片符号使这特别方便:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
words
['defenestrate', 'cat', 'window', 'defenestrate']
</pre>
4.3. 函数
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> for i in range(5):
... print(i)
...
0
1
2
3
4
</pre>
给定的终点不是生成的序列的一部分; range(10)
生成10个值,即长度为10的序列的项的合法索引。可以让范围从另一个数字开始,或者指定一个不同的增量(即使是负数,有时这称为“step”):
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">range(5, 10)
5 through 9
range(0, 10, 3)
0, 3, 6, 9
range(-10, -100, -30)
-10, -40, -70
</pre>
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb
</pre>
如果你只打印 range,会出现奇怪的结果:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> print(range(10))
range(0, 10)
</pre>
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> list(range(5))
[0, 1, 2, 3, 4]
</pre>
后面我们将看到更多的函数返回iterables和接受iterables作为参数。
4.4. 和语句,以及循环中子句
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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 number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
</pre>
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> for num in range(2, 10):
... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found a number", num)
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9
</pre>
4.5. 语句
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> while True:
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
</pre>
这通常用于创建最小的类:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> class MyEmptyClass:
... pass
...
</pre>
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> def initlog(*args):
... pass # Remember to implement this!
...
</pre>
4.6. 定义函数
我们可以创建一个函数,将斐波纳契(Fibonacci)序列写入任意边界:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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
</pre>
函数定义在当前符号表中引入函数名。函数名的值具有被解释器识别为用户定义函数的类型。此值可以分配给另一个名称,然后也可以用作一个函数。这作为一个重命名机制:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> fib
<function fib at 10042ed0>
f = fib
f(100)
0 1 1 2 3 5 8 13 21 34 55 89
</pre>
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> fib(0)
print(fib(0))
None
</pre>
很容易写一个返回Fibonacci系列数字列表的函数,而不是打印它:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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]
</pre>
这个例子,像往常一样,演示了一些新的Python功能:
-
语句可以从函数中携带着返回值返回。 语句不携带任何表达式参数时返回
None
。如果一直执行到整个函数结束还没有碰到 return 语句,也返回None
。 - 语句
result.append(a)
调用了列表result
的一个 方法。方法是“属于”一个对象且名字叫做obj.methodname
的函数,其中obj
是某个对象(可能是个表达式),methodname
是由该对象类型定义的方法的名称。不同的类型定义不同的方法。不同的类型的方法可以具有相同的名称而不引起歧义。(可以使用classes定义你自己的对象类型和方法,参见)示例中显示的append()
方法是给列表对象定义的;它添加一个新的元素到列表的末尾。在这个示例中,它等同于result = result + [a]
,但是它更高效。
4.7. 更多关于定义函数
也可以定义具有可变数量参数的函数。有三种形式,可以组合。
4.7.1. 默认参数值
最有用的形式是为一个或多个参数指定默认值。这创建了一个函数,可以使用比定义允许的参数少的参数进行调用。例如:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">def ask_ok(prompt, retries=4, reminder='Please try again!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise ValueError('invalid user response')
print(reminder)
</pre>
该函数可以通过几种方式调用:
- 只提供必须的参数:
ask_ok('Do you really want to quit?')
- 提供可选参数中的一个:
ask_ok('OK to overwrite the file?', 2)
- 或者提供所有参数:
ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')
默认值在函数定义的时刻,在定义的作用域中计算,因此:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">i = 5
def f(arg=i):
print(arg)
i = 6
f()
</pre>
会打印 5
.
重要的警告︰默认值只初始化一次。当默认值是一个可变对象(如列表,字典或大多数类的实例)时,默认值会不同。例如,以下函数累积在后续调用中传递给它的参数:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
</pre>
这将打印
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">[1]
[1, 2]
[1, 2, 3]
</pre>
如果你不想在后续调用之间共享默认值,你可以写这样的函数:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
</pre>
4.7.2. 关键字参数
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">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, "!")
</pre>
接受一个必需的参数 (voltage
) 和三个可选参数 (state
, action
, and type
)。此函数可以通过以下任一方式调用:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
</pre>
但所有以下调用将无效:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">parrot() # required argument missing
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument
parrot(110, voltage=220) # duplicate value for the same argument
parrot(actor='John Cleese') # unknown keyword argument
</pre>
在函数调用中,关键字参数必须写在位置参数之后。传递的所有关键字参数必须匹配函数接收的参数中的一个(例如,actor
不是parrot
函数的合法参数),它们的顺序并不重要。这同样适用于非可选的参数(例如,parrot(voltage=1000)
也是合法的)。没有参数可以多次接收到值。以下是由于此限制而失败的示例:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> def function(a):
... pass
...
function(0, a=0)
Traceback (most recent call last): File "<stdin>", line 1, in ?
TypeError: function() got multiple values for keyword argument 'a'
</pre>
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">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)
keys = sorted(keywords.keys())
for kw in keys:
print(kw, ":", keywords[kw])
</pre>
它可以这样调用:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")
</pre>
当然它会打印:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch
</pre>
注意,在打印关键字参数内容之前,它的名称列表是通过排序字典的keys()
方法得到的关键字创建的;如果不这样做,打印出来的参数顺序将是未定义的。
4.7.3. 任意参数的列表
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">def write_multiple_items(file, separator, *args):
file.write(separator.join(args))
</pre>
通常,这些可变的
参数将位于形式参数列表的最后面,因为它们将剩下的传递给函数的所有输入参数都包含进去。出现在*args
之后的任何形式参数都是“非关键字不可”的参数,意味着它们只能用作关键字参数而不能是位置参数。
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> def concat(*args, sep="/"):
... return sep.join(args)
...
concat("earth", "mars", "venus")
'earth/mars/venus'
concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
</pre>
4.7.4. 分拆参数列表
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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]
</pre>
同样的风格,字典可以通过**
操作传递关键字参数:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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 !
</pre>
4.7.5. Lambda 表达式
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> def make_incrementor(n):
... return lambda x: x + n
...
f = make_incrementor(42)
f(0)
42
f(1)
43
</pre>
上面的例子使用一个lambda表达式来返回一个函数。另一个用法是传递一个小函数作为参数:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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')]
</pre>
4.7.6. 文档字符串
这里有一些关于文档字符串的内容和格式的约定。
第一行应始终是对象的目的的简短摘要。为了简洁,它不应该显式地声明对象的名称或类型,因为这些可以通过其他方式(除非名称恰好是描述函数的操作的动词)。此行应以大写字母开头,并以句点结尾。
如果文档字符串中有更多行,第二行应为空白,将摘要与其余描述视觉上分开。以下行应该是描述对象的调用约定,其副作用等的一个或多个段落。
Python解析器不会在Python中从多行字符串字面值中删除缩进,因此处理文档的工具必须剥离缩进。使用以下约定即可,字符串第一行之后的第一个非空行确定整个文档字符串的缩进量。(我们不能使用第一行,因为它通常与字符串的开头引号相邻,所以它的缩进在字符串字面值中不明显。)这个缩进的空格“等价”然后从字符串的所有行的开始被去除。缩进较少的行不应该出现,但如果它们出现,它们的所有前导空白都应该被删除。扩展标签后,应测试空格的等效(通常为8个空格)。
下面是一个多行docstring的例子:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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.
</pre>
4.7.7. 函数注解
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 18.528px; border: 1px solid rgb(170, 204, 153); font-family: monospace, sans-serif; font-size: 15.44px; border-radius: 3px;">>>> 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'
</pre>
4.8. 插曲:代码风格
现在你将要编写更长,更复杂的Python部分,现在是讨论编码风格的好时机。大部分语言都可以有多种(比如更简洁,更格式化)写法,有些写法可以更易读。让你的代码更具可读性,而良好的编码风格对此有很大的帮助。
-
使用4空格缩进,没有制表符。
4个空格是小缩进(允许更大的嵌套深度)和大缩进(更容易阅读)之间的良好折衷。标签引入混乱,最好省略。
-
换行,使其不超过79个字符。
这有助于用户使用小型显示器,并可以在较大的显示器上并排放置多个代码文件。
-
使用空行来分隔函数和类,以及函数中更大的代码块。
-
如果可能,注释单独成行。
-
使用文档字符串。
-
在操作符两边和逗号之后加空格, 但不要直接在左括号后和右括号前加:
a = f(1, 2) + g(3, 4)
. -
如果您的代码要在国际环境中使用,不要使用花哨的编码。Python 默认的 UTF-8 或者 ASCII 在任何时候都是最好的选择。
-
同样,只要存在哪怕一丁点可能有使用另一种不同语言的人会阅读或维护你的代码,就不要在标识符中使用非 ASCII 字符。
脚注