With Python 3.8 one can simply use f-string debugging feature:
>>> foo = dict()
>>> f'{foo=}'.split('=')[0]
'foo'
One drawback of this method is that in order to get 'foo' printed you have to add f'{foo=}' yourself. In other words, you already have to know the name of the variable. In other words, the above code snippet is exactly the same as just
>>> 'foo'
Answer from Aivar Paalberg on Stack Overflowpython - Getting the name of a variable as a string - Stack Overflow
Dynamic variable name
Follow these rules to write PYTHON variables names like a PRO.
How do i display the variable's name instead of it's value
Videos
With Python 3.8 one can simply use f-string debugging feature:
>>> foo = dict()
>>> f'{foo=}'.split('=')[0]
'foo'
One drawback of this method is that in order to get 'foo' printed you have to add f'{foo=}' yourself. In other words, you already have to know the name of the variable. In other words, the above code snippet is exactly the same as just
>>> 'foo'
We can look at the dictionary of local variables via inspect's currentframe. We can then pull out the name(s) of the variable(s) that match the value passed in. Because we're doing this in its own method, we'll need to jump back to the caller's scope (so add a .f_back).
Altogether that'll look like:
import inspect
x, y, z = 1, 2, 3
def get_var_name(var):
callers_local_vars = inspect.currentframe().f_back.f_locals.items()
return [var_name for var_name, var_val in callers_local_vars if var_val is var]
print(retrieve_name(y))
(Note that if there is more than one variable assigned to the same value, you will get both of those variable names)
If you're calling this function from another function, something like:
def foo(bar):
return retrieve_name(bar)
foo(baz)
And you want the baz instead of bar, you'll just need to go back a scope further. This can be done by adding an extra .f_back in the caller_local_vars initialization.
See an example here: ideone
-
We have to agree that variables names are an essential part of the source code and meaningful names are important when it comes to program comprehension.
-
They serve as an implied documentation that should convey to the reader;
-
Considering that sometimes names are the only documentation, then clean code approach should be highly regarded.
-
Therefore, it is important to devote a considerable amount of time to the issue of variable naming and therefore the variable names and rules.
Variable Names Rules & Guidelines
-
A variable name CAN be one word, though not a Must
-
You SHOULD separate words with underscores and not spaces.
-
When naming variable's we SHOULD only use alpha-numeric characters (letters and numbers) and the underscore character.
-
You SHOULD NOT start a variable name with a number, but can start with a letter or an underscore character. Breaking this rule will give you an invalid decimal literal or syntax error
-
A variable name SHOULD start with a letter or the underscore character.
-
You SHOULD NOT use python keywords and function names as variable names.
-
Variable names are case sensitive message, Message, MESSAGE and MeSsAge are different
-
Ensure that variable names are; Short: though they can be long. Descriptive: That is can tell what the variable is used for.
-
Take note that some letters like lowercase l and uppercase letter O can easily be confused with number 1 and 0.
-
While it is legal to use upper case letters; Others think it is a good idea to start variable names with lower case letter. Others believe that they better use camel case than separate names with_ . Others avoid starting variables names with an underscore, unless when they are writing library code.
References and further reading.
Watch this for code examples
copper = 0.2 Iron = 0.5
So if i print copper or iron , it shows " 0.2 "and " 0.5 " , but i wanna display "copper" and "iron" any ways i can do that ?