0

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.

3
  • 1
    The variables in your code are not valid identifiers. Run the first line in Python's interactive mode and you'll get a syntax error right away. Please test for things like this before submitting questions, as it makes it much more difficult for people to help you. Commented Aug 23, 2011 at 22:47
  • Sorry for that.I was intending to put a pseudocode instead of real code. Commented Aug 24, 2011 at 0:19
  • See this link as another solution. Commented May 24, 2013 at 11:17

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why this is very likely to fail and not recommended ? Can you give me a scenario?
It's hard to give an example without a broader picture of why you're trying to do this at all, but in general this will give you strange behavior if other names are assigned to the objects alpha or beta (even if indirectly by assigning using elements of gamma) or if alpha or beta are reassigned to something else. Also, things like this result in code that's hard to read. Python is a language for writing explicit code with obvious behavior. If you want to do magic tricks, consider switching to Ruby or Perl. They're much better at them.
@goodside - +1, you're right, its possible :) Here's a oneliner to catch all possible variable names, assuming the gc is tracking them: def varnames(val): print frozenset().union(*[set(k for (k,v) in d.iteritems() if v is val) for d in gc.get_referrers(y) if type(d) == dict])
0
  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

2 Comments

You can surely, as seen in here.
@Developer yes, it's possible, but not by traversing the stack - you'd need to iterate over all objects like goodside suggests.
0

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
    …

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.