I am writing a program for school and I have been wondering if there is a function that print variable names and not their values.
a=2131
b="sdfds"
c=[a, b]
print(c)
i want to print "a" and "b"
I am writing a program for school and I have been wondering if there is a function that print variable names and not their values.
a=2131
b="sdfds"
c=[a, b]
print(c)
i want to print "a" and "b"
Try this method:
def what(var, locals=locals()):
for name, value in list(locals.items()):
if value is var:
return name
Usage:
>>> a=2131
>>> b="sdfds"
>>> what(a)
'a'
>>> what(b)
'b'
>>> c=[a, b]
>>> for i in c:
... what(i)
...
'a'
'b'
>>>