Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 772
TinyTheBrontosaurus Avatar asked Nov 01 '17 16:11

TinyTheBrontosaurus


People also ask

How do you mark an unused variable in Python?

To suppress the warning, one can simply name the variable with an underscore ('_') alone. Python treats it as an unused variable and ignores it without giving the warning message.

How to remove unused variable warning in Python?

To avoid such warnings, the convention of naming the unused variable with an underscore('_') alone can be used. This avoids the problem of unused variables in for loops. Consider the following script with an unused loop variable tested with the Vulture module in Python.

What is an unused variable?

Unused variables are a waste of space in the source; a decent compiler won't create them in the object file. Unused parameters when the functions have to meet an externally imposed interface are a different problem; they can't be avoided as easily because to remove them would be to change the interface.


3 Answers

Just del them. It is lightning fast because of the way garbage collector works.

def test(test):
    del test

    print('I do not need test parameter here!')

If you are passing parameters with callback method then give them a proper name and del them. Do not denote them as unused.

This is an example callback function:

def handle(socket, address):
    del address  # del is as efficient as a newline ;-)

    stream = bytes()
    while True:
        chunk = socket.recv()
        if not chunk:
            break

        stream += chunk

    return stream

Pythonistas normally do not use _ underscore name for an argument is any case possible.
You may have misunderstood usage of _ underscore as a name for a non useful variable.

It is understandable to use _ for a variable name when we do not know how to call it and/or it will not get used:

# ignore variables when unpacking a tuple
def test():
    """some code here"""

    top = 10
    right = 10
    bottom = 40
    left = 10

    return top, right, bottom, left


# here we ignore right and left
top, _, bottom, _ = test()

# use top and bottom in your code
like image 138
Elis Byberi Avatar answered Nov 01 '22 14:11

Elis Byberi


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.

like image 40
jmd_dk Avatar answered Nov 01 '22 13:11

jmd_dk


Pylint (and most likely other readers of your code) will be as happy if you concatenate several underscores. Pylint won't complain about unused arguments if you do this:

def example_non_adjacent(_, used, __):
    return used

I agree with some commenters in which this is ugly and I would try to avoid it by all means.

Pylint (and most human readers, I guess) won't complain either if you add the prefix cb_ to your function names to convey the fact that they are callbacks and you have to receive some arguments even if you do not want to use them. This looks like a better solution to me.

def cb_example_non_adjacent(unused1, used, unused2):
    return used
like image 3
Stop harming Monica Avatar answered Nov 01 '22 15:11

Stop harming Monica