39

Possible Duplicate:
How can I get around declaring an unused variable in a for loop?

In Python what is the best practice for naming a variable that is not going to be used? This is an odd question sure, but for my specific case I have a tuple of (key, value) where I am only interested in the value. A basic demo of my use case:

some_stuff = [("key", "value"), ("key2", "value")]
for _, value in some_stuff:
    pass  # TODO: do something really important with value

I've noticed in Eclipse that naming a variable _ or with an _ prefix (e.g. _k) will not show an unused variable warning and removing the _ prefix causes the warning to be raised, but this may just be an oddity/"feature" in Eclipse rather than Python best practice..

Community
  • 1
  • 1
Tom Hennigan
  • 1,082
  • 1
  • 9
  • 17
  • 1
    I personally would use the `for key, value in some_stuff:` also in the case when the `key` is not to be used. It can be used in future, anyway. The variable should tell what it contains. The variable should not say *I am not expected to be used*. – pepr Jul 14 '12 at 18:49
  • For future visitors: [PEP 640 -- Unused variable syntax](https://www.python.org/dev/peps/pep-0640/) – user26742873 Jul 31 '21 at 05:57

4 Answers4

36

Not sure if this is a Eclipse thing or not, but I generally use '_' to denote values I don't care about (i.e., return values in tuples, or index values in for-loops).

Of course you can always resort to old stand-bys like naming variables dummy or ignore.

I'm not sure if PEP 8 mentions anything about this, might be worth peeking into.

Levon
  • 138,105
  • 33
  • 200
  • 191
  • yeah I had a quick glance at PEP-8 but couldn't see a mention of this topic in there. – Tom Hennigan Jul 14 '12 at 18:17
  • @TomHennigan I also took a look, esp at this part, [naming conventions](http://www.python.org/dev/peps/pep-0008/#naming-conventions), but didn't see anything either. – Levon Jul 14 '12 at 18:19
  • 10
    keep in mind python uses '_' as a magic variable in the shell ... it is also common for strings to be translated be marked as `_("some string")` – Joran Beasley Jul 14 '12 at 18:21
  • @JoranBeasley good point, but only when a program is set up to use [gettext](http://docs.python.org/library/gettext.html#module-gettext) or something similar - by default this method doesn't exist. – dimo414 Jul 14 '12 at 18:22
  • @JoranBeasley You made the comment about string translation [here](http://stackoverflow.com/questions/11421997/in-python-what-the-underline-parameter-mean-in-function/11422019#11422019) too, and I meant to ask if you could elaborate (forgot to follow up then). I know about the use of `'_'` in the shell of course :) – Levon Jul 14 '12 at 18:24
  • 2
    @Levon see here :http://docs.python.org/library/gettext.html @dimo414 you are correct for translations however teh python shell uses it to store last value eg `>>>5+3\n>>>print _` would print 8 – Joran Beasley Jul 14 '12 at 18:27
  • 2
    all that said as long as you are not using the shell or translations using '\_' for an ignore_me type variable is very common and absolutely ok (and even if you are using translations you can map it to something else ,\_ is just convention) – Joran Beasley Jul 14 '12 at 18:29
20

This is a Python coding convention, yes. Best practice is to use _ where you don't care about the value being set, for example when unpacking values from a tuple. Sometimes, however, needing this is a sign you may be doing something else in a non-Pythonic way.

_ as a prefix is used to indicate "private" methods and variables, like Phil Cooper said. Use this to indicate that these methods are not part of any public contract other modules can or should rely on.

Some references:

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Actually, only one of the usages of `_` means *unused* variable. Another used explanation is *the last used value* in some construct, or the implicit name of a variable with the result of the last operation. So, the opinions differ, and then it should not be generally uses for the purpose. – pepr Jul 14 '12 at 18:44
  • 2
    In the interactive Python shell `_` is available for convenience as the last used value. In Python scripts, [this is not the case](http://stackoverflow.com/questions/5995572/python-underscore-variable). It cannot be relied on to provide that functionality in all cases, but it can, and by convention is, used to clearly denote "I do not care about the current value of this variable" throughout Python. – dimo414 Jul 14 '12 at 19:48
4

Let's start with...

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
...
Readability counts.
Special cases aren't special enough to break the rules.

I recommend to choose the approach that is not questionable. It usually means something that is self-explainable, simple and understandable. If you insist on expressing the not used, what about the name unused or dummy?

Should the _ be more invisible, hidden or even the candidate for to be overlooked, if possible?

For Perl users, the _ means something else than unused. Even though you may not care about the Perl users, what would the _ mean for you if you never met it before?

pepr
  • 20,112
  • 15
  • 76
  • 139
  • 1
    that's an interesting way of putting it, and I would tend to agree. I guess at least when someone new to python comes to your code and reads through they don't jump on google looking for the special meaning of _ in python if the variable is just called key. – Tom Hennigan Jul 14 '12 at 19:25
  • 1
    you're right though, I have no great cares for Perl users. – Tom Hennigan Jul 14 '12 at 19:25
2

You pretty much got it with the _1:

  • one leading underscore means, yes, this exists, and you can look at it, but another method or property is what you should use instead; consult the docs.
  • two leading underscores means, danger! Do not touch or you will seriously break the state and stability of things. Please stay away; if you mess with it, it's all your own fault if something goes wrong.

Note: Using __ is more than just a naming convention as it causes name mangling. This renames the variable to _ClassName__var. That makes the variable/method harder to use outside of the class. It's primarily done to avoid accidental overrides of methods in parent classes by inherited classes.

Joe McMahon
  • 3,266
  • 21
  • 33
Phil Cooper
  • 5,747
  • 1
  • 25
  • 41
  • is the __ common for local variables (e.g. in a function or loop) or just instance variables where you want to mangle the name for semi-privateness? I don't think I've seen that used anywhere other than instance vars. – Tom Hennigan Jul 14 '12 at 18:29
  • No. It is not typically used for local variables as the __ is meant to denote something special. varibables in functions, loops etc will be garbage collected when the function returns so no need to worry. Better to give meaningful names without the _ modifiers/flag – Phil Cooper Jul 14 '12 at 18:38
  • 1
    -1. `__` prefix is for name mangling. Using it for anything else is not good practice for python. `_` prefix means "please don't touch this" - the convention is that if you rely on such attributes outside of the class, you risk things breaking. – Daenyth Jul 14 '12 at 18:42
  • @Daenyth -1...really. I thought that's what I already said. Sometimes I aint' such a good talker though. – Phil Cooper Jul 14 '12 at 18:50
  • 1
    The current text implies that it can/should be used for OOP "private"-ness, which is a common mistake. It's used to avoid name conflicts and tie the variable to one specific class name. – Daenyth Jul 14 '12 at 18:59