Trying to set up dictionary to map to functions

Randy Belt randybelt at gmail.com
Tue Dec 8 15:51:07 EST 2009


Hi,

I have a small test program written trying to set up a dictionary that
points keys to functions.  It is working.  However, in the process of
creating it I noticed a weird problem.  The problem is that this IS WORKING
and I think it shouldn't be.

~ Here is the input config file code ~  its called config.file and is
referenced from the script.

[MAPS]
relmap = 1
posmap = 1
asnmap = 1

~ Here is the code that is working but I feel that it shouldn't be ~

import ConfigParser
config = ConfigParser.ConfigParser()
config.read("config.file")
sections = config.sections()
print config.options('MAPS')
def posmap():
    print "posmap function"
def relmap():
    print "relmap function"
def asnmap():
    print "asnmap function"
for value in config.options('MAPS'):
    value
map_library = {
               'posmap': posmap()
              ,'relmap': relmap()
              ,'asnmap': asnmap()
              }

~ Output ~

['posmap', 'relmap', 'asnmap']
posmap function
relmap function
asnmap function

~ The reason I'm confused is because when I change the code (Take away the
map_library dictionary)

import ConfigParser
config = ConfigParser.ConfigParser()
config.read("config.file")
sections = config.sections()
print config.options('MAPS')
def posmap():
    print "posmap function"
def relmap():
    print "relmap function"
def asnmap():
    print "asnmap function"
for value in config.options('MAPS'):
    value

~ The output is the following ~

['posmap', 'relmap', 'asnmap']

Is this defaulting to the dictionary and making it work?  In the first set
of code I don't reference the map at all but it still seems to know where to
look?  I am considerably new to Python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091208/04b8624d/attachment.html>


More information about the Python-list mailing list