下划线_作为Python中的变量名[重复]

时间:2022-05-02 13:28:01

This question already has an answer here:

这个问题在这里已有答案:

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

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

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

(为了我的眼睛,我添加了一些空格,CR和标签;向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?

在评论的正下方有一行以“_,s”开头。这似乎是具有s的最小值的解包元组(len(values [s]),s)。 Norvig博士是否使用“_”作为变量名称只是为了表明它是“不关心”的结果,还是其他事情正在发生?是否有时候建议将“_”作为变量名?在交互模式下,“_”代表前一个操作的答案;在非交互式代码中是否有类似的功能?

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.

谢谢你的好答案。我猜答案是Alex Martelli的“增值”;他指出,“_,vbl_of_interest”成语通常是DSU成语的副作用,而这本身就很大程度上是不必要的。

3 个解决方案

#1


64  

Yep, _ is a traditional name for "don't care" (which unfortunately *es with its use in I18N, but that's a separate issue;-). BTW, in today's Python, instead of:

是的,_是“不关心”的传统名称(不幸的是它在I18N中的使用发生冲突,但这是一个单独的问题;-)。 BTW,在今天的Python中,而不是:

_,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;-).

(不确定Python正在编写什么版本的Python,但他使用的成语是“decorate-sort-undecorate”[[DSU]]的例子,除了min而不是sort,在今天的Python中key = optional参数是一般来说做DSU的最好方法;-)。

#2


8  

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.

你是对的。在非交互模式中_没有特殊含义。实际上,Norvig只是想表达他并不关心那个变量的价值。

Offtopic: That article by Norvig is very nice. A recommended read.

Offtopic:Norvig的那篇文章非常好。推荐阅读。

#3


8  

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.

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

#1


64  

Yep, _ is a traditional name for "don't care" (which unfortunately *es with its use in I18N, but that's a separate issue;-). BTW, in today's Python, instead of:

是的,_是“不关心”的传统名称(不幸的是它在I18N中的使用发生冲突,但这是一个单独的问题;-)。 BTW,在今天的Python中,而不是:

_,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;-).

(不确定Python正在编写什么版本的Python,但他使用的成语是“decorate-sort-undecorate”[[DSU]]的例子,除了min而不是sort,在今天的Python中key = optional参数是一般来说做DSU的最好方法;-)。

#2


8  

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.

你是对的。在非交互模式中_没有特殊含义。实际上,Norvig只是想表达他并不关心那个变量的价值。

Offtopic: That article by Norvig is very nice. A recommended read.

Offtopic:Norvig的那篇文章非常好。推荐阅读。

#3


8  

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.

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