How to specify multiple unused values ​​in Python?

Normally in Python, you should use _ to indicate that the argument is not used.

 def example_basic(unused): pass 

becomes

 def example_basic(_): pass 

Then, if there are several unused arguments, several _ cannot be used because they conflict, so *_ :

 def example_multiple(unused1, unused2): pass 

becomes

 def example_multiple(*_): pass 

Finally, what if several non-contiguous arguments are not used?

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

Using multiple _ still does not work, and using *_ will not work because they are not adjacent.

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

EDIT:

I posted an example where it is needed here

+7
python pep8 pylint
source share
4 answers

I saw codes using the following idiom;

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

which I find nice if you really have a lot of unused variables.

However, simply because the variable is not used, this does not mean that the code is more readable if you do not specify its own name. This should only be done if you really think that hiding variable names improves the readability and / or understanding of the code.

+5
source share

Pilint (and most likely other readers of your code) will be just as happy if you combine several underscores. Pylint will not complain about unused arguments if you do this:

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

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

Pylint (and most readers, I think) will not complain if you add the cb_ prefix to your function names to indicate that they are callbacks, and you need to get some arguments even if you don't want to use them. It looks like the best solution for me.

 def cb_example_non_adjacent(unused1, used, unused2): return used 
+3
source share

I strongly agree with @jmd_dk on this. Just because the function does not actually reference or change the argument does not mean that it is not used. In the end, it must be created and explicitly passed to the function. The only reasonable use of underscores for variable names is the use of the for -loops and list methods:

 numbers = {_ for _ in range(10)} for _ in numbers: print("Foo!") 

But the fact that you need such a solution means that your code has design problems.

+1
source share

Just del them. It is lightning fast due to the way the garbage collector works.

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

If you pass parameters using the callback method, give them your own name and del them. Do not indicate 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 usually do not use the _ underscore name for the argument anyway.
You may have misunderstood the use of _ underscore as the name for a non-useful variable.

It is clear to use _ for the variable name when we do not know how to call it and / or it will not be 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 
+1
source share

All Articles