Getting current variable name

Ron radam2 at tampabay.rr.com
Thu Mar 17 14:28:05 EST 2005


Jeff Shannon wrote:
> 
> Are you sure that you really need that single-element list?

No I'm not sure, I thought I found a concdition where it made a 
difference while playing with it, but I don't recall just what 
circumstance it was?

> Don't forget, in Python, all names are references.  You only have to be 
> careful when you start re-binding names...

Since more than one name can bind to an object, this would be better.


def getvinfo( vars, v ):
     names = []
     for n in vars.keys():
         if vars[n] is v:
             names.append(n)
     return names, v, type(v)

a = [2]
b = [2]
c = b

print getvinfo( locals(), a )
print getvinfo( locals(), b )
print getvinfo( locals(), c )

 >>>
(['a'], [2], <type 'list'>)
(['b', 'c'], [2], <type 'list'>)
(['b', 'c'], [2], <type 'list'>)
 >>>
















More information about the Python-list mailing list