Names of lists/dicts

Hi, I am working on a script where I need to use a lot of aux commands for printing content of a list or a dictionary like this:

my_list = [1,3,5,2]
....................................
print "len(my_list) = ",len(my_list),"   my_list = ",my_list
# or (alternatively):
# print "len(my_list) = "+str(len(my_list))+"   my_list = "+str(my_list)

So it is needed to repeat (copy-paste or type) 4 times the name of the list/dict I am interested in… I thought if I have a function to tell me list’s name, I can use a much simple procedure with just 1 name of that list such as:

def Output_data(lst):
  lst_name = Get_Name(lst)  # pseudo code - a function that gives me list's name in the script
  print "len("+lst_name+") = "+str(len(my_list))+"   "+lst_name+" = "+str(lst)

Obviously, in the code above, lst_name needs to be a string.
But I dont know how to achieve this… :eek:
Any suggestions?

Well, according to a thread like this, that’s the wrong way of thinking about it, since an object in python can have multiple references / names. It might be possible, I don’t know, but I’d choose to do something simpler:


def Output_data(lst, lst_name):
  print "len("+lst_name+") = "+str(len(lst))+"   "+lst_name+" = "+str(lst)

# Then call it like this:
Output_data(Some_list, "Some_list")

Humm, well… I’ve also produced this implementation but with reverse order of arguments - first the string (list_name), then the vairable list/dict… But I was looking for a proc which I can call by only 1 argument - the list/dict itself… :frowning:

Apparently, there is no such opportunity as there seems to be NO .name attribute built-in Python… I wasn’t completely sure about this!!!

Thanks though :slight_smile:

There are no name or hash attributes you can read (or set) in a list. Reflection in Python is rather limited, and I haven’t found such functions. If you really must, you can create a subclass of list that has a name attribute. If you’re not going to use it a lot, tho, what ThatSoundAgain suggests is more practical.

From slight googling, I gather that you can get the global variable space as a dictionary: globals(). You could then probably compare that to the list.

Hold on; Here:


def output_list(lst):
	lst_name = 'name not found'
	g = globals()
	for var in g:
		if g[var] == lst:
			lst_name = var
			break
	
	print "len("+lst_name+") = "+str(len(lst))+"   "+lst_name+" = "+str(lst)

list_example = ["2","gh"]
output_list(list_example)

Does what you want. But I still think the other way is better / faster / more readable. This could get quite slow if you have a large number of objects in global scope, and it won’t behave predictably if you have the same list referenced several times.

Or as an alternative:


def getnames(o):
    i=id(o)
    return [name for name,value in globals().iteritems() if id(value)==i]

This returns a list of possible names (assuming them to be in the global namespace - otherwise it gets tricky because in the local namespace of getnames, the object is called “o”. If you just wanted the first name (or knew there would only be one reference to that object) then this would work:


def getnames(o):
    i=id(o)
    return [name for name,value in globals().iteritems() if id(value)==i][0]

Hope that makes sense.

Neil.

@ ThatSoundAgain & neil - Wow, guys, that’s really weird and working! :)))

Thinking logically, since there is “globals()”, there should be also “locals()”… Since globals() refer to global vars, procs, modules, etc, then locals() should refer to your local ones :spin:

Based on the logic above, I was able to construct the following proc:

def GetName(o,d):
    i=id(o)
    return [name for name,value in d.iteritems() if id(value)==i][0]

where d is a dictionary to search in… practically, it can be global or local… but applied to local spaces of the following set-up:

def Test_1():
    vector = (0,0,1)
    st = GetName(vector,locals())
    print st
    print locals()[st]

def Test_2():
    vector = (0,0,2)
    st = GetName(vector,locals())
    print st
    print locals()[st]

def Test_3():
    vector = (0,0,3)
    st = GetName(vector,locals())
    print st
    print locals()[st]
    Test_1()
    Test_4()
    st = GetName(vector,locals())
    print st
    print locals()[st]
    
def Test_4():
    vector = (0,0,4)
    st = GetName(vector,locals())
    print st
    print locals()[st]

print "Tests........"
Test_1()
Test_2()
Test_3()

it gives this output:

Tests........
vector
(0, 0, 1)
vector
(0, 0, 2)
vector
(0, 0, 3)
vector
(0, 0, 1)
vector
(0, 0, 4)
vector
(0, 0, 3)

The last four pairs come from the Test_3 proc :wink:

Therefore, my proc(s) I was looking originally may become something like this:

def Output_list_new(data,d):
    data_name = GetName(data,d)
    print "len("+data_name+") = "+str(len(data))+"   "+data_name+" = "+str(data)

This will help me pretty much in scripts debugging :slight_smile:

Thanks again and regards!