Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the name of a given variable in python?

Tags:

python

object

I am programming in python and need to access the name I have given to an object so as to be able to pass this as a string (concatenated with another string).

The reason I need to do this is that the program I am using forces me to create a global (which in my case is a dictionary) and I am writing a function to work in general with several different objects (which each have similar sets of properties, eg. object 1 is of length 2 (signifying in my case 2 neurons) labelled 0 and 1 and each of these has 4 properties a,b,c,d. I want to create a "dictionary filetree" of these properties but both object1 and object 2 are 2 instances of the same class, therefore I need to change the first level keys to being 'NAME1_0, 'NAME1_1', 'NAME2_0', 'NAME2_1').

def Init(neuron,input):
#Initialises the neuron group, arguements are neuron (neurongroup) and input (neurongroup)
global dict
dict={}
for x in range(0,len(neuron)):
    neuron[x].L=0
    neuron[x].G=0
    neuron[x].ron=1/period
    neuron[x].roff=1/period
    dict[x]={'tau_on':0.5,
             'Non_off':neuron[x].roff*0.5,
             'Non_off':neuron[x].ron*0.5,
             'Ni_on':ones(len(input))*qon*0.5,
             'Ni':ones(len(input))*qoff*0.5+ones(len(input))*qon*0.5,
             'p_current':0,
             'p_previous':0,
             'my_tau_on':[],
             'my_Non_off':[],
             'my_Noff_on':[],
             'my_Ni_on':[],
             'my_Ni':[],
             'my_p_on':[],
             'my_ron':[],
             'my_roff':[],
             'my_theta':[],
             'my_weights':[],
             'my_record_times':[]}
     dict['%s' % x, neuron]=dict.pop(x)

does not work as it does not give the name assigned to the object but merely the name of the object itself. for a more minimal case of my problem

NAME1=4
def func(x):    #creates string of 'NAME1'
    print 'x'

func(NAME1)
#output='NAME1'
like image 900
Mike H-R Avatar asked May 21 '26 19:05

Mike H-R


1 Answers

Your could do a reverse lookup of the names in globals():

>>> NAME1 = 4
>>> def name_of(value):
        for k, v in globals().items():
            if v is value:
                return k
        raise KeyError('did not find a name for %s' % value)

>>> name_of(NAME1)
'NAME1'

If the same object has been assigned to more than one name, only one name will be returned.

If you want to search more broadly than just globals, look for the dicts in gc.get_referrers(value) and capture all matches:

>>> def names_of(value):
        'Find all names for the value'
        result = []
        for d in gc.get_referrers(value):
            if isinstance(d, dict):
                for k, v in d.items():
                    if v is value:
                        result.append(k)
        return result

>>> import math
>>> pie = math.pi
>>> names_of(pie)
['pie', '_pi', 'pi', 'pi']
like image 181
Raymond Hettinger Avatar answered May 24 '26 09:05

Raymond Hettinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!