Unused variable naming in python

Question:

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..

Asked By: Tom Hennigan

||

Answers:

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.

Answered By: Levon

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.

Answered By: Phil Cooper

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:

Answered By: dimo414

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?

Answered By: pepr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.