What is the meaning of _ after for in this code?
if tbh.bag:
n = 0
for _ in tbh.bag.atom_set():
n += 1
本文探讨了Python中单下划线变量的三种主要用法:作为交互式解释器中上一条执行语句的结果;用于国际化的翻译查找;以及作为丢弃变量的名称,用于表示函数结果的一部分被故意忽略。
|
210
68
|
What is the meaning of
| |||
|
314
|
The latter two purposes can conflict, so it is necessary to avoid using | ||||||||||||||||||||||||||||||||
|
|
94
|
It's just a variable name, and it's conventional in python to use | ||||||||||||
|
|
20
|
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):
(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 " UpdateThanks 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. | ||||
marked as duplicate by poke Nov 12 '14 at 20:48This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. | |||||
| |||||
|
49
|
Yep,
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 | ||||||||||||||||||||
|
|
8
|
You are correct. In non-interactive mode Offtopic: That article by Norvig is very nice. A recommended read. | ||||
|
|
6
|
Your interpretation is correct. Outside of the special meaning in interactive mode |
|
20
|
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):
(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 " UpdateThanks 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. | ||||
marked as duplicate by poke Nov 12 '14 at 20:48This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. | |||||
| |||||
|
49
|
Yep,
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 | ||||||||||||||||||||
|
|
8
|
You are correct. In non-interactive mode Offtopic: That article by Norvig is very nice. A recommended read. | ||||
|
|
6
|
Your interpretation is correct. Outside of the special meaning in interactive mode |
3万+
1033
len(tbh.bag.atom_set())(if the returned value has a__len__method) orsum(1 for _ in tbh.bag.atom_set())– Nick T Apr 5 at 20:08