Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I access the name of a given variable in python?
    primarykey
    data
    text
    <p>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). </p> <p>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').</p> <pre><code>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) </code></pre> <p>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</p> <pre><code>NAME1=4 def func(x): #creates string of 'NAME1' print 'x' func(NAME1) #output='NAME1' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload