How can you print a variable name in python?
If you insist, here is some horrible inspect-based solution.
import inspect, re
def varname(p):
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\bvarname\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
if m:
return m.group(1)
if __name__ == '__main__':
spam = 42
print varname(spam)
I hope it will inspire you to reevaluate the problem you have and look for another approach.
Python: Print a variable's name and value?
You can just use eval
:
def debug(variable):
print variable, '=', repr(eval(variable))
Or more generally (which actually works in the context of the calling function and doesn't break on debug('variable')
, but only on CPython):
from __future__ import print_function
import sys
def debug(expression):
frame = sys._getframe(1)
print(expression, '=', repr(eval(expression, frame.f_globals, frame.f_locals)))
And you can do:
>>> x = 1
>>> debug('x + 1')
x + 1 = 2
Python - How to print the variable name of an Object
Note that an object may be referred to as multiple names.
It is also possible that there is no object name referring to the object.
Below is one approach that achieves your goal. It uses globals()
, the dictionary that stores mappings from names to objects inside the global environment. Essentially, the __str__
method searches the object in the global listings (so it can be very slow if there are many objects) and keeps the name if matches.
You could possibly use locals
instead to narrow the search scope.
In the example, C
is referring to the same object as A
. So print(C)
tells both A
and C
are the names.
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
results = []
for name, obj in globals().items():
if obj == self:
results.append(f' "{name}" has the points: ({self.x}, {self.y})')
return "; ".join(results)
A = Point()
B = Point()
print(A)
#"A" has the points: (0, 0)
print(B)
# "B" has the points: (0, 0)
C = A
print(C)
# "A" has the points: (0, 0); "C" has the points: (0, 0)
Python: print variable name and value easily
From Python 3.8 there is a = for f-strings:
#!/usr/bin/env python3
python="rocks"
print(f"{python=}")
This would output
# python=rocks
How to print variable name in a list
You can't do this with plain variables. Use a dictionary instead:
mydict = {
'Andi': 100,
'Bill': 50,
'Kira': 25
}
for name in mydict:
print(name, mydict[name])
Python get variable name instead of value
Though i don't think there is any way (or i don't know) but an easy way is to store them in a dict like this;
my_dict = {'Type1': 1, 'Type2': 2}
Then to get the variable name, you just need to loop over the my_dict
and get all the keys and simply use them as you want.
for key, value in my_dict.items():
print(key)
this will give an output like this;
Type1
Type2
Print variable name in python
you cannot get the name of a variable as string, but you can pass a string in input and query locals
dictionary to get the value:
for data in ["bkd_train", "bkd_test"]:
print("{} : {}".format(data,locals()[data].shape))
it works if the variables are local. If they're global, use globals
, with fallback on locals
, or just eval
(not a problem in that context since the strings to evaluate are constant)
also read: How to get a variable name as a string?
Getting the name of a variable as a string
TL;DR
Use the Wrapper
helper from python-varname
:
from varname.helpers import Wrapper
foo = Wrapper(dict())
# foo.name == 'foo'
# foo.value == {}
foo.value['bar'] = 2
For list comprehension part, you can do:
n_jobs = Wrapper(<original_value>)
users = Wrapper(<original_value>)
queues = Wrapper(<original_value>)
priorities = Wrapper(<original_value>)
list_of_dicts = [n_jobs, users, queues, priorities]
columns = [d.name for d in list_of_dicts]
# ['n_jobs', 'users', 'queues', 'priorities']
# REMEMBER that you have to access the <original_value> by d.value
I am the author of the python-varname
package. Please let me know if you have any questions or you can submit issues on Github.
The long answer
Is it even possible?
Yes and No.
We are retrieving the variable names at runtime, so we need a function to be called to enable us to access the previous frames to retrieve the variable names. That's why we need a Wrapper
there. In that function, at runtime, we are parsing the source code/AST nodes in the previous frames to get the exact variable name.
However, the source code/AST nodes in the previous frames are not always available, or they could be modified by other environments (e.g: pytest
's assert
statement). One simple example is that the codes run via exec()
. Even though we are still able to retrieve some information from the bytecode, it needs too much effort and it is also error-prone.
How to do it?
First of all, we need to identify which frame the variable is given. It's not always simply the direct previous frame. For example, we may have another wrapper for the function:
from varname import varname
def func():
return varname()
def wrapped():
return func()
x = wrapped()
In the above example, we have to skip the frame inside wrapped
to get to the right frame x = wrapped()
so that we are able to locate x
. The arguments frame
and ignore
of varname
allow us to skip some of these intermediate frames. See more details in the README file and the API docs of the package.
Then we need to parse the AST node to locate where the variable is assigned value (function call) to. It's not always just a simple assignment. Sometimes there could be complex AST nodes, for example, x = [wrapped()]
. We need to identify the correct assignment by traversing the AST tree.
How reliable is it?
Once we identify the assignment node, it is reliable.
varname
is all depending on executing
package to look for the node. The node executing detects is ensured to be the correct one (see also this).
It partially works with environments where other AST magics apply, including pytest, ipython, macropy, birdseye, reticulate with R, etc. Neither executing nor varname is 100% working with those environments.
Do we need a package to do it?
Well, yes and no, again.
If your scenario is simple, the code provided by @juan Isaza or @scohe001 probably is enough for you to work with the case where a variable is defined at the direct previous frame and the AST node is a simple assignment. You just need to go one frame back and retrieve the information there.
However, if the scenario becomes complicated, or we need to adopt different application scenarios, you probably need a package like python-varname
, to handle them. These scenarios may include to:
- present more friendly messages when the source code is not available or AST nodes are not accessible
- skip intermediate frames (allows the function to be wrapped or called in other intermediate frames)
- automatically ignores calls from built-in functions or libraries. For example:
x = str(func())
- retrieve multiple variable names on the left-hand side of the assignment
- etc.
How about the f-string
?
Like the answer provided by @Aivar Paalberg. It's definitely fast and reliable. However, it's not at runtime, meaning that you have to know it's foo
before you print the name out. But with varname
, you don't have to know that variable is coming:
from varname import varname
def func():
return varname()
# In external uses
x = func() # 'x'
y = func() # 'y'
Finally
python-varname
is not only able to detect the variable name from an assignment, but also:
- Retrieve variable names directly, using
nameof
- Detect next immediate attribute name, using
will
- Fetch argument names/sources passed to a function using
argname
Read more from its documentation.
However, the final word I want to say is that, try to avoid using it whenever you can.
Because you can't make sure that the client code will run in an environment where the source node is available or AST node is accessible. And of course, it costs resources to parse the source code, identify the environment, retrieve the AST nodes and evaluate them when needed.
Related Topics
Unix Socket Credential Passing in Python
Differencebetween "Is None" and "== None"
Is _Init_.Py Not Required for Packages in Python 3.3+
How to Set Time Limit on Raw_Input
Differencebetween Dict.Items() and Dict.Iteritems() in Python2
How to Preserve Timezone When Parsing Date/Time Strings with Strptime()
How to Switch to New Window in Selenium for Python
How to Capture Mouseevents and Keyevents Using Python in Background on Linux
Construct Pandas Dataframe from Items in Nested Dictionary
How to "Perfectly" Override a Dict
Why Does This Code for Initializing a List of Lists Apparently Link the Lists Together
What Is the Most Efficient Way to Loop Through Dataframes with Pandas