I've written about the multiple usages of the underscore _ in Python and in that article I write about the fact that the underscore is idiomatically used to assign a value we don't care about.
Something like this:
## Suppose we have a colour like `colour = ("red", (255, 0, 0))`
colour_name, _ = colour
The code above uses unpacking to extract the colour name from the variable colour and we use the second variable name _ to idiomatically say โwe expect there to be a second value to unpack, but we don't care about its valueโ.
But this is just a convention.
There is nothing special about the underscore _ in this situation; it's just a valid variable name:
_ = 3
_ *= 2
print(_) # 6
However, in Python's 3.10 match statement, the underscore _ was turned into a soft keyword.
This means that when you write a match statement containing a case statement that looks like case _:, Python actually parses the underscore _ as a keyword!
So, if _ can be used as a regular variable name and if _ can be parsed as a keyword, depending on the context, that makes it a soft keyword!
To confirm that _ can be parsed as a keyword inside a case statement, don't take my word for it.
You can open Python's grammar and see for yourself!
Get ready for 12 intense days of problem-solving. The โAlgorithm Mastery Bootcampโ starts December 1st and it will feature 24 programming challenges, live analysis sessions, a supportive community of like-minded problem-solvers, and more! Join now and become the Python expert others can rely on.