<br>I can&#39;t see the forest through the trees.<br><br>I have stored 3 global variables in a dictionary, and associated each variable with a filename.<br>Ideally, I want to read the contents of the text files, and store the contents in the global variables. The globals will be used by another function.
<br>However, when I do the assignment to varname = fh.readlines(), a new variable is created, and the reference to the global variable is overwritten, because the contents of the files are strings, and strings are immutable.
<br><br>I see the problem, but not a good solution.<br><br><br>var1=&quot;&quot;<br>var2=&quot;&quot;<br>var3=&quot;&quot;<br><br>def initGlobals():<br>&nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp; global var1, var2, var3<br><br>&nbsp;&nbsp;&nbsp; fileDict = {&#39;var1.txt
&#39;:var1, &#39;var2.txt&#39;:var2, &#39;var3.txt&#39;:var3}<br><br>&nbsp;&nbsp;&nbsp; for fn, varname in fileDict.iteritems():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fh=open(fn, &#39;r&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #id(varname) # at this point, this id matches the id of the global variable
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varname = fh.readlines() # this creates a new variable, but I want to store the file contents in the global var<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #id(varname)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # this is a new id, the global var is not updated
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fh.close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except IOError:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\nFATAL ERROR occurred reading %s\n&quot; % fn<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fh.close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>