# 变量
# 标识符
- 下划线 _ 具有特殊作用:
- 编程时,可以将不会使用的临时变量命名为 _ ,提醒读者不需要关心该变量。
for _ range(10): print('循环十次')
- 在终端交互式编程时,如果执行某条语句的返回值不为 None ,则会自动赋值给名为 _ 的变量。
[root@CentOS ~]# python3 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> _ # 此时变量 _ 尚未创建 File "<stdin>", line 1, in <module> NameError: name '_' is not defined >>> 1 1 >>> _ # 此时变量 _ 被自动赋值了 1 >>> print(2) # 执行该语句的返回值为 None ,不会赋值给变量 _ 2 >>> _ 1
- 进行 I18N 国际化、L10N 本地化时,通常将翻译字符串的函数命名为 _ ,便于调用。
def _(message): return message print(_('hello world'))
- 编程时,可以将不会使用的临时变量命名为 _ ,提醒读者不需要关心该变量。