0

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.

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22
  • 1
    `_var` already has the meaning of "private variable", either as relating to the `import *` syntax, or just as a hint to other programmers. There's nothing preventing you from ascribing some new meaning to it, but beware that it _may_ be confusing to others. – deceze Aug 23 '23 at 02:32
  • To whoever closed this. I made it extremely clear that I was talking about local variables. This is not a duplicate of the other pages, which are talking about private fields. I also made it clear that there is some support for this meaning in linters, but I wasn't sure why. @deceze – Frank Yellin Aug 23 '23 at 02:32
  • 1
    I have linked you to two duplicates which address *what `_var` already means.* Not attributes, but actual local variables. That explains why this syntax isn't an error. And it also shows why the meaning you are trying to ascribe to it _may_ be misunderstood. – deceze Aug 23 '23 at 02:36

0 Answers0