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'
Your could do a reverse lookup of the names in globals():
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: