1

I'm trying to access a variable's name. For example, if I call a function to print a list (printL) and want to print my list's name "A_minus_B":

def printL (xlist):
    print "****************************"
    print "name of list"
    print (" entries :  "),len(xlist) 
    print xlist
    print  "****************************"
    return()

How can I get the function to print "A_minus_B" instead of "name of list", the name I used when calling the function printL(A_minus_B)?

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Thomas_r
  • 27
  • 5

2 Answers2

1

You can't.* The detailed reasons are discussed in the Python language reference, but basically what Python does when you call printL(A_minus_B) is take the value that has been labeled A_minus_B and create a new label, xlist, that happens to refer to the same thing. From that point on, the label xlist has absolutely no connection to the other label A_minus_B, and in fact, the value which has been labeled A_minus_B or xlist or whatever has no connection to any of the names that have been used to label it.


*Well, you can, by using some deep "Python voodoo"... you would basically have to tell Python to read the piece of source code where you call printL(A_minus_B) and extract the variable name from there. Normally that's the kind of thing you probably shouldn't be doing (not because you're a beginner, but because it usually indicates you've designed your program the wrong way), but debugging is one of those cases where it's probably appropriate. Here's one way to do it:

import inspect, pprint

def print_values(*names):
    caller = inspect.stack()[1][0]
    for name in names:
        if name in caller.f_locals:
            value = caller.f_locals[name]
        elif name in caller.f_globals:
            value = caller.f_globals[name]
        else:
            print 'no such variable: ' + name
        # do whatever you want with name and value, for example:
        print "****************************"
        print name
        print (" entries :  "),len(value) 
        print value
        print  "****************************"

The catch is that when you call this function, you have to pass it the name of the variable you want printed, not the variable itself. So you would call print_values('A_minus_B'), not print_values(A_minus_B). You can pass multiple names to have them all printed out, for example print_values('A_minus_B', 'C_minus_D', 'foo').

David Z
  • 128,184
  • 27
  • 255
  • 279
  • thanks David. How should I design this type of thing then? "When calling a function, tell me the name of the parameter I passed to that function". I suppose I could do listDic = {"listA" : list_A, "listB" : list_A} then call printL(listDic(mylist)), and have printL display mylist. Is this bad design? – Thomas_r Dec 18 '11 at 07:11
  • It depends, why do you need the name of the variable? – David Z Dec 18 '11 at 07:32
  • I have a function to print values of my lists (soe sort of debugging) and want to print out the name of the list I had passed to the function. The dictionnary approach seems to work. I have a listDic = {"list_A": listA, "list_B": listB} I call printL("listX"), which print "listX" then print the values in listX by doing print listDic[listX]. Is this sound coding? Many thanks Thomas – Thomas_r Dec 18 '11 at 16:20
  • (I'm doing this here because in the question that was linked as a duplicate in heltonbiker's comment, this would not be a good design choice.) – David Z Dec 19 '11 at 01:15
0

Alternate way to do it:

list_dict = {}     #creates empty dictionary
list_dict['list_a'] = [1,2,3,4,5,6]   # here you create the name (key)
nice_name = 'list_b'
list_dict[nice_name] = [7,8,9,10,11,12]   # here you use already existing string as name (key)

def list_info(ld):
    for key in sorted(ld):
        print "****************************"
        print key
        print " entries :  " ,len(ld[key]) 
        print ld[key]
        print  "****************************"

list_info(list_dict)
heltonbiker
  • 26,657
  • 28
  • 137
  • 252