How do I access the name of a given variable in python?

Your could do a reverse lookup of the names in globals(): NAME1 = 4 >>> def name_of(value): for k, v in globals().items(): if v is value: return k raise KeyError('did not find a name for %s' % value) >>> name_of(NAME1) 'NAME1 If the same object has been assigned to more than one name, only one name will be returned If you want to search more broadly than just globals, look for the dicts in gc. Get_referrers(value) and capture all matches: def names_of(value): 'Find all names for the value' result = for d in gc. Get_referrers(value): if isinstance(d, dict): for k, v in d.items(): if v is value: result.

Append(k) return result >>> import math >>> pie = math. Pi >>> names_of(pie) 'pie', '_pi', 'pi', 'pi'.

Your could do a reverse lookup of the names in globals(): >>> NAME1 = 4 >>> def name_of(value): for k, v in globals().items(): if v is value: return k raise KeyError('did not find a name for %s' % value) >>> name_of(NAME1) 'NAME1' If the same object has been assigned to more than one name, only one name will be returned. If you want to search more broadly than just globals, look for the dicts in gc. Get_referrers(value) and capture all matches: >>> def names_of(value): 'Find all names for the value' result = for d in gc.

Get_referrers(value): if isinstance(d, dict): for k, v in d.items(): if v is value: result. Append(k) return result >>> import math >>> pie = math. Pi >>> names_of(pie) 'pie', '_pi', 'pi', 'pi'.

2 An up-vote, but I would cry to see this used. – pst Oct 26 at 5:28 This is so awesomely evil, it makes me want to laugh, cry and cheer all at once. – Michael Anderson Oct 26 at 5:46 2 You should call gc.collect() before gc.

Get_referrers to avoid potential "extras". From the docs: "Note that objects which have already been dereferenced, but which live in cycles and have not yet been collected by the garbage collector can be listed among the resulting referrers. To get only currently live objects, call collect() before calling get_referrers()." – Michael Anderson Oct 26 at 5:49.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions