Get Variable Name As String In Python
In Python, getting the name of a variable as a string is not as straightforward as it may seem, as Python itself does not provide a built-in function for this purpose. However, several clever techniques and workarounds can be used to achieve this. In this article, we will explore some simple methods to obtain the variable name as a string in Python.
Get Variable Name As String In Python
Below, are the example of Get Variable Name As String In Python.
- Using locals() Function
- Using globals() Function
- Using a custom class
- Using inspect Module
Get Variable Name As String Using locals() Function
In this example, the below code defines a function, `get_var_name`, that returns the name of a local variable given its value using the `locals()` dictionary. The example demonstrates using the function to find and print the name of a variable (`my_variable`) along with its type.
def get_var_name(var):
for name, value in locals().items():
if value is var:
return name
# Example usage:
my_variable = 42
variable_name = get_var_name(my_variable)
print(f"The variable name is: {variable_name}")
print(type(variable_name))
Output
The variable name is: var <class 'str'>
Get Variable Name As String Using globals() Function
In this example, below code defines a function, `get_var_name`, which retrieves the name of a global variable given its value from the global namespace using `globals()`. The example demonstrates using this function to find and print the name of a variable (`my_variable`) along with its type.
def get_var_name(var):
for name, value in globals().items():
if value is var:
return name
# Example usage:
my_variable = 42
variable_name = get_var_name(my_variable)
print(f"The variable name is: {variable_name}")
print(type(variable_name))
Output
The variable name is: my_variable <class 'str'>
Get Variable Name As String Using a Custom Class
In this example, code defines a class `VariableWrapper` with attributes `value` and `name`. An instance of this class, `my_variable`, is created with a value of 42 and a name of "my_variable." The example then prints the name attribute of `my_variable` along with its type.
class VariableWrapper:
def __init__(self, value, name):
self.value = value
self.name = name
# Example usage:
my_variable = VariableWrapper(42, "my_variable")
print(f"The variable name is: {my_variable.name}")
print(type(my_variable.name))
Output
The variable name is: my_variable <class 'str'>
Get Variable Name As String Using inspect Module
In this example, below code defines a function, `get_var_name`, which uses the inspect module to obtain the name of a variable given its value. The example demonstrates using this function to find and print the name of a variable (`my_variable`) along with its type.
import inspect
def get_var_name(var):
current_frame = inspect.currentframe()
caller_frame = inspect.getouterframes(current_frame)[1]
local_vars = caller_frame.frame.f_locals
for name, value in local_vars.items():
if value is var:
return name
# Example usage:
my_variable = 42
variable_name = get_var_name(my_variable)
print(f"The variable name is: {variable_name}")
print(type(variable_name))
Output
The variable name is: my_variable <class 'str'>