So I know that it is common in Python to use _
to indicate a variable whose value is being ignored. So for example:
first, *_, last = my_list
In some code I've been looking at, I've started to see _var
being used instead of just _
for unused local variables. So for example, a method that overrides a superclass's method might have:
class MyLogger:
def log(self, message, _urgency):
...
to make it clear what is being elided. And if a subclass wants to override this and bring that argument back, it is clearer what is going on. Similarly:
name, _address, city, state = my_list
makes it clearer what's being skipped, and helps if the code needs to be modified in the future.
Both PyLint and PyCharm don't issue warning messages for unused local variables starting with _
, so this is clearly a "thing", even if it's not sanctioned.
I'm trying to figure out whether this use of an underscore on local variables is at all sanctioned or part of any PEP or anything. This use of _ isn't going to be confused with private fields, all of which have to have an object and a period before the underscore. Private globals will typically be uppercase.