Underscore variable with walrus operator in Python

183 Views Asked by At

In Python, the variable name _ (underscore) is often used for throwaway variables (variables that will never be used, hence do not need a proper name).

With the walrus operator, :=, I see the need for a variable that is rather short lived (used in say only one line of code). I wonder if the use of _ is reasonable to use also in this case, or if it might be confusing for someone reading the code?

Example:

a = (dummy := pd.Series([1, 2, 3, 4, 5]))[dummy > 2]

Here a pandas series is created and immediately filtered. The name dummy could in my opinion be replaced with _:

a = (_ := pd.Series([1, 2, 3, 4, 5]))[_ > 2]
3

There are 3 best solutions below

1
Tom McLean On BEST ANSWER

You are using the variable dummy, to filter the series. Therefore, don't replace it with _.

0
Lazyer On

I think it isn't enough reasonable to use underline because it has already been used twice in the code.

Even if you don't use the dummy variable anymore, it's less readable in the code now.

0
Nikolaj Š. On

My point of view differs from guys'. _ isn't really special as a name, it's a convention mostly for linters to ignore "unused variable" warnings. So it can be used like you suggested. A somewhat convoluted example:

>>> [_ := i ** 2
...  for i in (6, 5, 4, 3, 2)
... ][_]
4

So you can absolutely use underscore in your example. Whether you should, is a question of personal preference. Authors of two earlier answers seem to dislike such usage, while I personally tend to favour it.

In my opinion, it indicates that this variable is not used outside of this line of code. It stands out visually enough for the line to be readable, while taking only one character. Some linters report one-letter variable names, so _ has an advantage there.

It might surprise those who are not accustomed to such usage of underscores, but it still is readable and intuitive enough not to be really confusing.

But then again, it is a personal preference.