Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it acceptable to use underscore as a loop variable in python?

As per the official python tutorial,

In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behaviour.

There are no confusions here.

Then I saw some people using _ as a loop variable. For example, as per this blog post:

_ is used as a throw-away name. This will allow the next person reading your code to know that, by convention, a certain name is assigned but not intended to be used. For instance, you may not be interested in the actual value of a loop counter:

n = 42
for _ in range(n):
    do_something()

Is this a good convention? I verified in the interpreter that using _ in loop masks the built-in variable afterwards. But is it okay to use it as a loop variable when it is used in scripts (ie. not in interactive mode)

like image 566
Hrishi Avatar asked Nov 07 '22 11:11

Hrishi


1 Answers

As a matter of long-standing convention in Python, _ is used as a name developer merely uses to fulfil syntactical requirements. Single-character variables have been often associated with throw-away names in many programming languages.

Such usage is acceptable when it is a clear signal that the return value stored in this variable is unimportant.


In the interactive mode

As you have noticed, the meaning of _ within Python's interpreter is similar to Ans in a typical modern calculator setup. When you use _ as a variable name, that feature is overshadowed. Nonetheless, it is reversable and after executing your code in the interactive mode:

n = 42
for _ in range(n):
    do_something()

You may discard this variable from the global name pool (as long as it is not referenced elsewhere) with del:

del _

The value of _ would reflect the last return value again.


Special cases

There are other recognized ways of using _ name, present chiefly in internationalization and localization.

The name _ is used as an alias of either of the two most commonly used functions of the gettext module, gettext.gettext and gettext.gettext_lazy. This usage has become popularized by the Django framework which utilizes that library in its internal utils package. For instance, when the string 'abc' needs to be translated, according to this convention it would be referred to in the following way:

from django.utils.translation import gettext as _
# ...
_('abc')  # this string would be translated

Due to that reason, certain Python figures, such as Kenneth Reitz, promote the usage of __ (double underscore) instead of _ (single underscore) as a throw-away variable without much appeal from the rest of the community though.

like image 147
Arn Avatar answered Nov 14 '22 21:11

Arn