Python 中下划线 _ 作为变量名

发布于 2024-08-11 09:31:32 字数 1193 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-08-18 09:31:32

是的,_ 是“不关心”的传统名称(不幸的是,这与其在 I18N 中的使用发生冲突,但这是一个单独的问题;-)。顺便说一句,在今天的 Python 中,

_,s = min( (len( values[s]), s) 
            for s in squares 
            if len(values[s]) > 1
        )

您可以编写代码

s = min((s for s in squares if len(values[s])>1), 
        key=lambda s: len(values[s]))

(不确定 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:

_,s = min( (len( values[s]), s) 
            for s in squares 
            if len(values[s]) > 1
        )

you might code

s = min((s for s in squares if len(values[s])>1), 
        key=lambda s: len(values[s]))

(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;-).

画离情绘悲伤 2024-08-18 09:31:32

你的解释是正确的。除了交互模式中的特殊含义外,_ 只是用作“不关心”的变量名,尤其是在解包时。

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.

木槿暧夏七纪年 2024-08-18 09:31:32

你是对的。在非交互模式下_没有特殊含义。事实上,诺维格只是想表达他并不关心该变量的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文