下划线_作为Python中的变量名 [英] Underscore _ as variable name in Python

查看:373
本文介绍了下划线_作为Python中的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Peter Norvig的文章描述了解决数独难题的程序,甚至是最难解决的难题,确定性的逻辑运算和可能的解决方案的智能遍历.后者是递归完成的;这就是该功能():

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]
            )

(为我的目光,我添加了一些空格,CR和制表符;向Norvig博士道歉.)

(I've added some spaces, CRs, and tabs for the sake of my eyes; apologies to Dr. Norvig.)

在评论的正下方有一行以"_,s"开头的行.这似乎是最小值为s的解压缩元组(len(values[s]),s). 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?

感谢您的答复.我猜答案是亚历克斯·马特利(Alex Martelli)的增值";他指出,"_,vbl_of_interest"惯用语通常是DSU惯用语的副作用,而在很大程度上,DSU惯用语本身就没有必要了.

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.

推荐答案

是的,_是无关紧要"的传统名称(很遗憾,它与I18N中的用法冲突,但这是一个单独的问题;- ).顺便说一句,在当今的Python中,代替:

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
        )

您可以编码

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

(不知道Peter Peter正在编写什么版本的Python,但是他使用的惯用法是"decorate-sort-undecorate" [[DSU]]的示例,除了用min代替了sort之外,在今天的Python中,可选参数通常是执行DSU的最佳方法;-).

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

这篇关于下划线_作为Python中的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆