Python 中下划线 _ 作为变量名
Peter Norvig 有一篇文章描述了一个通过结合确定性逻辑运算来解决数独谜题的程序,即使是最难的谜题并智能遍历可能的解决方案。后者是递归完成的;这是该函数(源):(
def search(values):
"Using depth-first search and propagation, try all possible values."
if values is False:
return False ## Failed earlier
if all( len( values[s]) == 1 for s in squares):
return values ## Solved!
## Chose the unfilled square s with the fewest possibilities
_,s = min( (len( values[s]), s)
for s in squares
if len(values[s]) > 1
)
return some( search( assign( values.copy(), s, d))
for d in values[s]
)
我添加了一些空格、CR 和制表符,以便我的眼睛;向诺维格博士道歉。)
在评论的正下方有一行以“_,s
”开头的行。这似乎是解压缩的元组 (len(values[s]),s
),其最小值为 s
。 Norvig 博士使用“_
”作为变量名只是为了表明这是一个“不关心”的结果,还是另有原因?有时是否建议使用“_
”作为变量名?交互模式下,“_
”保存的是上一次操作的答案;非交互式代码中是否有类似的功能?
更新
感谢您的好答案。我猜亚历克斯·马泰利(Alex Martelli)的答案是“增值”;他指出,“_, vbl_of_interest”习惯用法通常是 DSU 习惯用法的副作用,而该习惯用法本身在很大程度上是不必要的。
Peter Norvig has an essay describing a program to solve sudoku puzzles, even the hardest ones, by combining deterministic logical operations and smart traversal of the possible solutions. The latter is done recursively; here's that function (source):
def search(values):
"Using depth-first search and propagation, try all possible values."
if values is False:
return False ## Failed earlier
if all( len( values[s]) == 1 for s in squares):
return values ## Solved!
## Chose the unfilled square s with the fewest possibilities
_,s = min( (len( values[s]), s)
for s in squares
if len(values[s]) > 1
)
return some( search( assign( values.copy(), s, d))
for d in values[s]
)
(I've added some spaces, CRs, and tabs for the sake of my eyes; apologies to Dr. Norvig.)
Right below the comment there's a line starting with "_,s
". That seems to be the unpacked tuple (len(values[s]),s
) with the minimal value of s
. Is Dr. Norvig using "_
" as a variable name just to indicate it's a "don't care" result, or is something else going on? Are there times when "_
" is recommended as a variable name? In interactive mode, "_
" holds the answer of the previous operation; is there a similar function in non-interactive code?
Update
Thanks for the good answers. I guess The Answer goes to Alex Martelli for "value added"; he points out that the "_, vbl_of_interest" idiom is often a side effect of the DSU idiom, which itself has been made largely unnecessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,
_
是“不关心”的传统名称(不幸的是,这与其在 I18N 中的使用发生冲突,但这是一个单独的问题;-)。顺便说一句,在今天的 Python 中,您可以编写代码
(不确定 Peter 是为哪个版本的 Python 编写的,但他使用的习惯用法是“decorate-sort-undecorate”[[DSU]] 的示例,除了使用 min 而不是排序,在当今的 Python 中,
key=
可选参数通常是执行 DSU 的最佳方式;-)。Yep,
_
is a traditional name for "don't care" (which unfortunately clashes with its use in I18N, but that's a separate issue;-). BTW, in today's Python, instead of:you might code
(not sure what release of Python Peter was writing for, but the idiom he's using is an example of "decorate-sort-undecorate" [[DSU]] except with min instead of sort, and in today's Python the
key=
optional parameter is generally the best way to do DSU;-).你的解释是正确的。除了交互模式中的特殊含义外,
_
只是用作“不关心”的变量名,尤其是在解包时。Your interpretation is correct. Outside of the special meaning in interactive mode
_
is just used as a "don't care" variable name, especially in unpacking.你是对的。在非交互模式下
_
没有特殊含义。事实上,诺维格只是想表达他并不关心该变量的值。Offtopic:Norvig 的那篇文章非常好。推荐阅读。
You are correct. In non-interactive mode
_
has no special meaning. Indeed, Norvig just wants to convey that he doesn't care about the value of that variable.Offtopic: That article by Norvig is very nice. A recommended read.