2

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]
DustByte
  • 651
  • 6
  • 16
  • Coincidentally, not long after asking this question I came across this topic in the Julia programming documentation on variables. For the fun of it I quote it here (from https://docs.julialang.org/en/v1/manual/variables as of July 15, 2022): "A particular class of variable names is one that contains only underscores. These identifiers can only be assigned values but cannot be used to assign values to other variables. More technically, they can only be used as an L-value, but not as an R-value". – DustByte Jul 15 '22 at 09:45

3 Answers3

1

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

Tom McLean
  • 5,583
  • 1
  • 11
  • 36
  • 1
    Fair enough. I suppose `_` should indeed only be used for variables that will *never* be referenced. – DustByte Jun 17 '22 at 10:45
0

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.

Lazyer
  • 917
  • 1
  • 6
  • 16
0

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.

Nikolaj Š.
  • 1,457
  • 1
  • 10
  • 17