What is the meaning of _
after for
in this code?
if tbh.bag:n = 0for _ in tbh.bag.atom_set():n += 1
up vote
210 down vote favorite
68
|
What is the meaning of
python variables naming-conventions underscores metasyntactic-variable
|
|||
add a comment
|
up vote
314 down vote
|
The latter two purposes can conflict, so it is necessary to avoid using |
||||||||||||||||||||||||||||||||
add a comment
|
up vote
94 down vote
|
It's just a variable name, and it's conventional in python to use |
||||||||||||
|
favorite
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.
python variables naming-conventions underscores metasyntactic-variable
|
||||
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. |
|||||
add a comment
|
up vote
49 down vote
|
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 |
||||||||||||||||||||
show
2 more comments
|
up vote
8 down vote
|
You are correct. In non-interactive mode Offtopic: That article by Norvig is very nice. A recommended read. |
||||
add a comment
|
up vote
6 down vote
|
Your interpretation is correct. Outside of the special meaning in interactive mode |
favorite
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.
python variables naming-conventions underscores metasyntactic-variable
|
||||
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. |
|||||
add a comment
|
up vote
49 down vote
|
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 |
||||||||||||||||||||
show
2 more comments
|
up vote
8 down vote
|
You are correct. In non-interactive mode Offtopic: That article by Norvig is very nice. A recommended read. |
||||
add a comment
|
up vote
6 down vote
|
Your interpretation is correct. Outside of the special meaning in interactive mode |
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