xxxxxxxxxx
#!/usr/bin/env python3
foo = 1
bar = 2
print(f"{foo=} {bar=}")
xxxxxxxxxx
def get_variable_name(var):
# Iterate through the global symbol table to find the name of a variable
for name, value in globals().items():
if value is var:
return name
return None
# Example usage:
my_variable = 42
variable_name = get_variable_name(my_variable)
print(variable_name)
xxxxxxxxxx
>>> first = 'John'
>>> last = 'Doe'
>>> print '%(first)s %(last)s' % globals()
John Doe