[Tutor] Returning a variable from a module
pd
pd@localhost.localdomain
Sun, 8 Jul 2001 20:52:36 +1000 (EST)
Hi,
I have a problem with functions/modules. My code is as follows:
<snip>
def get(filename):
"""Gets values from a config file.
This function scans a single config file in the format:
[Config Filename]
key1=value1
key2=value2
...
keyN=valueN
Note: each configuration option is on a new line,
and there is no space on either side of the equal
sign. It dumps the value into a dictionary called
config which is a global variable. It is assumed
that the config file is an ascii text file."""
import string
file = filename
configFile= open(file, 'r')
configList = configFile.readlines()
configFile.close()
for n in range(0, len(configList)):
configList[n] = string.rstrip(configList[n])
configList[n] = string.lstrip(configList[n])
configList[n] = string.replace(configList[n], '\012', '')
global config
config = {}
for n in range(1, len(configList)):
splitString = string.split(configList[n], '=')
config[splitString[0]] = splitString[1]
</snip>
When I cut and paste the code listed below, and call the function with:
get('.gamerc')
It seems to work, but when I save this code into a file called
configlib.py and try to get values from the variable config. It doesnt
seem to work.
import configlib
configlib.import('.gamerc')
This leads to my question: How do I access a variable that I have created
within a module which I have imported?