Danny Staple Danny Staple - 1 month ago 5x
Python Question

Is "*_" an acceptable way to ignore arguments in python

If I have a function/method that is an implementation of a callback for some framework, and do not care for any further arguments, it seems to be syntactically correct, and to not have pylint/IDE complaints to use *_ to express no interest in any further arguments. The point I think is to express intent to both the tools,and other developers that these arguments are not currently relevant.

To clarify I mean:

def my_callbacK_handler(a, b, *_):

I've not seem this idiom used in the wild - is it common, are there examples and are there known problems with this?

For those not familiar - "_" expresses the intent that I am not interested in that symbol - it is the python "dummy" that is recognized by IDE's and linters.


It's just a variable like any other, but by convention it means that you don't intend to use that value, just read it and ignore it.

[OrderedSet() for _ in xrange(n)]

builds a list of n empty ordered sets. _ is the index, but is unused; _ is commonly used as required, but unused, variable name (not only in python). This is just a tricky python idiom, because there is no builtin syntax to do this.

it's not uncommon to see this in other languages (where _ is a valid identifier). _ often means a variable one's not interested in the value of, but which is needed for syntactic reasons.

Note that _(...) as a macro call has another conventional meaning, which comes from gettext, where one uses _("string literal") to indicate a string that needs localization.

A thread to read from ActivesState

I can not say about any problems, python doesn't use it internally,, its just a variable, its us who have to be careful.


The syntax is the * and **. The names *args and **kwargs are only by convention but there's no need not to use them.

def my_callbacK_handler(a, b, *_):

So what i understand if I see this function in your code by _ idiom, that this function only use a and b arguments in its working and will ignore others.

As you state " *_ to express no interest in any further arguments "