在 python 中使用下划线作为循环变量是否可以接受?
Is it acceptable to use underscore as a loop variable in python?
根据官方python tutorial,
In interactive mode, the last printed expression is assigned to the
variable _. This means that when you are using Python as a desk
calculator, it is somewhat easier to continue calculations, for
example:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
This variable should be treated as read-only by the user. Don’t
explicitly assign a value to it — you would create an independent
local variable with the same name masking the built-in variable with
its magic behaviour.
这里没有混淆。
然后看到有人用_
作为循环变量。例如,根据 this blog post:
_ is used as a throw-away name. This will allow the next person reading your code to know that, by convention, a certain name is
assigned but not intended to be used. For instance, you may not be
interested in the actual value of a loop counter:
n = 42
for _ in range(n):
do_something()
这个约定好吗?我在解释器中验证了在循环中使用 _
之后会屏蔽内置变量。但是当它在脚本中使用时(即不在交互模式下)将它用作循环变量是否可以
作为 Python 中长期存在的约定,_
用作开发人员仅用于满足句法要求的名称。在许多编程语言中,单字符变量通常与一次性名称相关联。
当明确表明存储在该变量中的 return 值不重要时,这种用法是可以接受的。
在交互模式中
如您所见,Python 解释器中 _
的含义类似于典型现代计算器设置中的 Ans
。当您使用 _
作为变量名时,该功能将被掩盖。尽管如此,它是可逆的,并且在以交互模式执行代码后:
n = 42
for _ in range(n):
do_something()
您可以使用 del
:
从全局名称池中丢弃该变量(只要它没有在别处被引用)
del _
_
的值将再次反映最后的 return 值。
特例
还有其他公认的使用 _
名称的方法,主要出现在国际化和本地化中。
名称_
用作gettext
模块的两个最常用函数gettext.gettext
和gettext.gettext_lazy
的别名。这种用法已被 Django 框架普及,该框架在其内部 utils
包中使用该库。例如,当字符串 'abc'
需要被翻译时,根据这个约定,它会被引用成下面的方式:
from django.utils.translation import gettext as _
# ...
_('abc') # this string would be translated
由于这个原因,某些 Python 人物,例如 Kenneth Reitz,提倡使用 __
(双下划线)而不是 _
(单下划线)作为抛出-away 变量对社区的其他人没有多大吸引力。
根据官方python tutorial,
In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behaviour.
这里没有混淆。
然后看到有人用_
作为循环变量。例如,根据 this blog post:
_ is used as a throw-away name. This will allow the next person reading your code to know that, by convention, a certain name is assigned but not intended to be used. For instance, you may not be interested in the actual value of a loop counter:
n = 42
for _ in range(n):
do_something()
这个约定好吗?我在解释器中验证了在循环中使用 _
之后会屏蔽内置变量。但是当它在脚本中使用时(即不在交互模式下)将它用作循环变量是否可以
作为 Python 中长期存在的约定,_
用作开发人员仅用于满足句法要求的名称。在许多编程语言中,单字符变量通常与一次性名称相关联。
当明确表明存储在该变量中的 return 值不重要时,这种用法是可以接受的。
在交互模式中
如您所见,Python 解释器中 _
的含义类似于典型现代计算器设置中的 Ans
。当您使用 _
作为变量名时,该功能将被掩盖。尽管如此,它是可逆的,并且在以交互模式执行代码后:
n = 42 for _ in range(n): do_something()
您可以使用 del
:
del _
_
的值将再次反映最后的 return 值。
特例
还有其他公认的使用 _
名称的方法,主要出现在国际化和本地化中。
名称_
用作gettext
模块的两个最常用函数gettext.gettext
和gettext.gettext_lazy
的别名。这种用法已被 Django 框架普及,该框架在其内部 utils
包中使用该库。例如,当字符串 'abc'
需要被翻译时,根据这个约定,它会被引用成下面的方式:
from django.utils.translation import gettext as _
# ...
_('abc') # this string would be translated
由于这个原因,某些 Python 人物,例如 Kenneth Reitz,提倡使用 __
(双下划线)而不是 _
(单下划线)作为抛出-away 变量对社区的其他人没有多大吸引力。