From this answer, you could do something like this
>>> a = 1
>>> for k, v in list(locals().iteritems()):
if v is a:
a_as_str = k
>>> a_as_str
a
>>> type(a_as_str)
'str'
That being said, this is not a good way to do it. Python objects do not have names. Instead, names have objects. A slight exception to this is the __name__
attribute of functions, but the rule is the same - we just store the name as an attribute. The object itself remains unnamed.
The method above abuses the locals()
method. It is not ideal. A better way would be to restructure your data into something like a dictionary. A dictionary is specifically a set of named values.
{
'street38': (4,6,7,12,1,0,5)
}
The point of a dictionary is this key-value relationship. It's a way to look up a value based on a name, which is exactly what you want. The other problem you may run in to (depending on what you're doing here) is what will happen if there are thousands of streets. Surely you don't want to write out thousands of variable names too.