Search code examples
pythonpylintpep8

How to indicate multiple unused values in Python?


Normally in Python, one should use an _ to indicate an argument is unused.

def example_basic(unused):
   pass

becomes

def example_basic(_):
   pass

Then if there are multiple unused arguments, multiple _s can't be used since they will conflict, so a *_ is used:

def example_multiple(unused1, unused2):
   pass

becomes

def example_multiple(*_):
   pass

Finally, what should be done if there are multiple non-adjacent arguments that go unused?

def example_non_adjacent(unused1, used, unused2):
    return used

Using multiple _s still does not work, and using *_ won't work since they're non-adjacent.

Note that I would very much prefer to change the API, but for the sake of this question let's assume that's not possible. Is there a way to indicate it's ignored without using something like # pylint: disable=unused-argument for PyLint or i-dont-know-what for PyCharm?

EDIT:

I posted an example where this is needed here


Solution

  • I've seen codes using the following idiom;

    def example_non_adjacent(_0, used, _1, _2, _3, also_used):
        ...
    

    which I find nice if you truly have lots of unused variables.

    That said, just because a variable is unused does not mean that the code is more readable if you leave out its proper name. This should only be done if you really think that hiding the variable names improve the readability and/or understanding of the code.