Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused variable naming in python [duplicate]

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

like image 278
Tom Hennigan Avatar asked Jul 14 '12 18:07

Tom Hennigan


People also ask

How do you handle unused variables in Python?

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.

Can you reuse variable names in Python?

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.

What is a throwaway variable in Python?

It's just another variable name. There's nothing being thrown away. – Vincent Savard.

Which variable names are illegal in Python?

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 _ )

How to identify unused variables in Python code?

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.

What are the naming conventions for variable names in Python?

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 (_).

What happens when you delete an unused variable in a function?

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.

What is dead code in Python?

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.


Video Answer


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

like image 97
Levon Avatar answered Sep 30 '22 02:09

Levon


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:

  • What is the purpose of the single underscore "_" variable in Python?
  • Underscores as function prefixes
  • What is the meaning of a single- and a double-underscore before an object name?
  • Why does python use two underscores for certain things?
like image 34
dimo414 Avatar answered Sep 30 '22 02:09

dimo414