How To Print A Variable’s Name In Python
Last Updated :
31 Jan, 2024
In Python, printing a variable’s name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable’s name, there are several creative ways to achieve this. In this article, we’ll explore five simple methods to print a variable’s name in Python.
Print A Variable Name In Python
Below are the example of How To Print A Variable’s Name In Python.
- Using locals() Function
- Using globals() Function
- Using a Custom Function
- Using inspect module
Print A Variable’s Name Using locals() Function
In this example, the below code defines a function `print_variable` that takes a variable as an argument, finds its name within the local scope using list comprehension with `locals()`, and prints the variable name.
Python3
def print_variable(variable):
variable_name = [name for name, value in locals ().items() if value is variable][ 0 ]
print (f "Variable name using locals(): {variable_name}" )
my_variable = 42
print_variable(my_variable)
|
Output
Variable name using locals(): variable
Print A Variable’s Name Using globals() Function
In this example, below code defines a function print_variable that takes a variable as an argument, finds its name within the global scope using list comprehension with globals(), and prints the variable name.
Python3
def print_variable(variable):
variable_name = [name for name, value in globals ().items() if value is variable][ 0 ]
print (f "Variable name using globals(): {variable_name}" )
global_variable = "Hello, World!"
print_variable(global_variable)
|
Output
Variable name using globals(): global_variable
Print A Variable’s Name Using a Custom Function
In this example, below code defines a function get_variable_name that takes an object and a namespace as arguments, finds the object’s name within the given namespace using list comprehension, and returns the variable name.
Python3
def get_variable_name(obj, namespace):
return [name for name, value in namespace.items() if value is obj][ 0 ]
custom_variable = [ 1 , 2 , 3 ]
custom_variable_name = get_variable_name(custom_variable, locals ())
print (f "Variable name using custom function: {custom_variable_name}" )
|
Output
Variable name using custom function: custom_variable
Print A Variable’s Name Using inspect Module
In this example, below code uses the inspect module to define a function `get_var_name` that takes a variable as an argument and prints its name by inspecting the local variables in the current frame. It then calls this function with a variable named `variable` having a value of 42. Note: Using `==` for value comparison might be more appropriate in this context instead of `is`.
Python3
import inspect
def get_var_name(var):
current_frame = inspect.currentframe()
try :
frame_locals = current_frame.f_back.f_locals
var_name = [name for name, value in frame_locals.items() if value is var][ 0 ]
print (f "Variable name: {var_name}" )
finally :
del current_frame
variable = 42
get_var_name(variable)
|
Output
Variable name: variable
Share your thoughts in the comments
Please Login to comment...