user845459
user845459

Reputation:

getting Python variable name in runtime

This is different from retrieving variable/object name at run time.

2G_Functions={'2G_1':2G_f1,'2G_2':2G_f2}
3G_Functions={'3G_1':3G_f1,'3G_2':3G_f2}
myFunctionMap=[2G_Functions,3G_Functions]
for i in myFunctionMap:
    print i.??? "\n"
    for j in i:
            print str(j)

I want the output look like below.

2G_Functions:

2G_1

2G_2

3G_Functions:

3G_1

3G_2

How can I get the name of dictionary variable in my code?I dont know which I am calling in the loop to know its name beforehand.

Upvotes: 0

Views: 998

Answers (4)

Doug Harris
Doug Harris

Reputation: 3379

Try making your list of lists as a list of strings instead:

d2G_Functions={'2G_1':"2G_f1",'2G_2':"2G_f2"}
d3G_Functions={'3G_1':"3G_f1",'3G_2':"3G_f2"}
myFunctions=["2G_Functions","3G_Functions"]

for dict_name in myFunctions:
    print dict_name
    the_dict = eval("d"+dict_name)
    for j in the_dict:
        print str(j)

(I changed the name of your original variables since python identifiers cannot begin with a digit)

Upvotes: 0

goodside
goodside

Reputation: 4629

Despite the pessimism of the other answers, in this particular case you actually can do what you're asking for if there are no other names names assigned to the objects identified by G2_Functions and G3_Functions (I took the liberty of fixing your names, which are not valid Python identifiers as given.) That being said, this is a terrible, terrible, terrible idea and you should not do it, because it will eventually break and you'll be sad. So don't do it. Ever.

The following is analogous to what you're trying to do:

alpha = {'a': 1, 'b': 2}
beta = {'c': 2, 'd': 4}
gamma = [alpha, beta]
listOfDefinedLocals = list(locals().iteritems())
for x, y in listOfDefinedLocals:
    if y is gamma[0]: print "gamma[0] was originally named " + x
    if y is gamma[1]: print "gamma[1] was originally named " + x

This will output:

gamma[1] was originally named beta 
gamma[0] was originally named alpha

I accept no responsibility for what you do with this information. It's pretty much guaranteed to fail exactly when you need it. I'm not kidding.

Upvotes: 3

David Wolever
David Wolever

Reputation: 154494

In short, you can't.

In longer, it is sort of possible if you poke deep into, I think, the gc module (for the general case) or use locals() and globals()… But it's likely a better idea to simply define the list like this:

myFunctionMap = [ ("someName", someName), … ]
for name, map in myFunctionMap:
    print name
    …

Upvotes: 0

orip
orip

Reputation: 75427

  1. You can't. The myFunctionMap list contains the objects, not the name attached to them 2 lines above. BTW, calling a list variable "map" isn't a good practice, maps are usually dictionaries.
  2. You can't start a variable name with a digit, so 2G_Functions and 3G_Functions won't work.
  3. You can sidestep the problem by creating a dictionary with appropriate names

e.g.

myFunctionMap = {
  "2G_Functions" : { ... },
  "3G_Functions" : { ... },
}

for (name, functions) in myFunctionMap.iteritems():
  print name
  for func in functions.keys():
    print func

Upvotes: 0

Related Questions