When using an underscore for a variable name, the Python Analyzer reports:
Line <number> [C0104 disallowed-name] was reported by Pylint:
Disallowed name "_.
Presumably, the disallowed name here (“_”) is supposed to be surrounded by quotes for clarity, but the closing quote appears to be missing.
Also, the feedback links to PEP 8, but I can’t find anything about underscores being discouraged in it, which is confusing.
Snippet of what triggered the feedback (full code can be seen in iteration 2 of my Grade School solution):
self.grades.extend(set() for _ in range(len(self.grades), grade + 1))
My understanding is that PEP 8 doesn’t say whether to use or not use _ as a standalone identifier specifically. It does however point out in several sections that underscores have very specific meanings in a handful of contexts. By documenting those situations , _ can then be used elsewhere to signal that we’re intending to ignore a value…
That out of the way, I’m of the opinion that PEP 8 is partially against _ in spirit. It says function and variable names should be words, separated by underscores as necessary to improve readability. _ isn’t separating two words so that isn’t a valid name.
1 Like
Hi @Yrahcaz7 
Thanks for reporting this! I will add the cleanup to my list.
As for why I left the rule active — Partially what @BNAndras has said above about variable names should be words.
But also — while using _ as a throwaway is fairly common, it does have some subtleties and conflicts. Not the least of which are interfering with gettext translations and creating confusion due to match-case using a single underscore as a wild card match.
Finally? It isn’t really throwaway. It is just a name that is used by convention to indicate indifference. But it is a name that is assigned, refcounted, and needs garbage collection. And it can be deadly if someone comes back and decides to reference it because if it has been used multiple times, who knows what it’s value is going to be?
So I file it under “looks cool but is (mostly) a bad idea”. I do sometimes use it, but often go back and slap a longer name on it.
Here are two articles that cover uses/pitfalls:
What is the Purpose of the single underscore? Stack Overflow
Underscores in Python by Shahriar Tajbakhsh
2 Likes
Adjusted the .pylintrc file and added the two links to the details file in Analyzer PR 84.
Not sure I can fix the missing quote mark - it appears to be upstream of the Analyzer in the PyLint code.
Oh - and I removed the reminder about different naming conventions, since it was pretty verbose. 
Here is the new message detail:
By default, Pylint enforces PEP8-suggested names. For more details on the pitfalls of _ specifically, see this Stack Overflow answer and Underscores in Python by Shahriar Tajbakhsh."
1 Like