clintval
clintval

Reputation: 369

Is __ (two underscores) favorable over _ (a single underscore) for a dummy variable?

I have an arbitrary example in Python 2.7 where I need a dummy variable. I found a lot of sources recommending the use of a single _ such as the below example:

 directory = {key: [0 for _ in range(catnum)] for key in register}

I found this link: http://docs.python-guide.org/en/latest/writing/style/#create-an-ignored-variable

Here is a quoted passage from the above URL:

Note: Many Python style guides recommend the use of a single underscore _ for throwaway variables rather than the double underscore __ recommended here. The issue is that _ is commonly used as an alias for the gettext() function, and is also used at the interactive prompt to hold the value of the last operation. Using a double underscore instead is just as clear and almost as convenient, and eliminates the risk of accidentally interfering with either of these other use cases.

I've just started coding in Python and was wondering if the __ is the accepted convention? What is an example of code where _ would cause an issue?

directory = {key: [0 for __ in range(catnum)] for key in blasts}

Upvotes: 3

Views: 890

Answers (2)

dano
dano

Reputation: 94951

Well, the quoted passage tells you exactly where using _ would be a problem: in the interactive interpreter, and in any code that's using it as an alias forgettext.gettext, which is common in applications that want to have their text localized. Here's the documentation for gettext.gettext (emphasis mine):

Return the localized translation of message, based on the current global domain, language, and locale directory. This function is usually aliased as _() in the local namespace.

Most programs use gettext.gettext like this:

import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print _('This is a translatable string.')

So that you don't actually have to type out gettext everywhere you want a translatable string.

Now, all that said, I see for _ in range(catnum) far more than I see for __ in range(catnum). I suppose you know if using _ is going to cause a problem; either you know your working in the interpreter, or you know your application uses _ as a gettext.gettext alias. Using __ is definitely safer, though.

If I were to guess why it hasn't caught on, I would say maybe people are just put off by __ because the double underscore is associated with Python's name-mangled "private" variables, which are generally frowned upon as unnecessary by the community. Just a guess, though.

Upvotes: 4

merlin2011
merlin2011

Reputation: 75585

As far as I have seen, most Python programs today still use _ as a dummy variable, instead of the double underscore __.

However, here is an example of a possible point of confusion that is described in the quoted section in your question:

>>> 1+1
2
>>> _
2
>>> 

Upvotes: 2

Related Questions