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..
To suppress the warning, one can simply name the variable with an underscore ('_') alone. Python treats it as an unused variable and ignores it without giving the warning message.
In Python, we may reuse the same variable to store values of any type. A variable is similar to the memory functionality found in most calculators, in that it holds one value which can be retrieved many times, and that storing a new value erases the old.
It's just another variable name. There's nothing being thrown away. – Vincent Savard.
Rules for Python variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Look at the code snippet below Some use pylint, a tool to keep track of code styles and dead code in Python. Such a tool can raise a warning when the variable in for loop is not used. To suppress that, it is better to use the underscore naming conventions for the unused variables.
Python naming conventions for variable names are same as function names. There are some rules we need to follow while giving a name for a Python variable. Rule-1: You should start variable name with an alphabet or underscore (_) character. Rule-2: A variable name can only contain A-Z,a-z,0-9 and underscore (_).
The unused variable can still be used when calling the function both as a positional argument and as a keyword argument. If you start to use it later, you can't since it's deleted, so there is less risk of mistakes. It's standard python syntax.
A code with many unused local variables, unused imports, or unused line of codes is considered to be dead code. When there are situations where you are ambiguous that a local variable may have a role later in the code, then there are some naming conventions that can be used to distinguish it from the rest of the variables.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With