Trying to set up dictionary to map to functions
Dave Angel
davea at ieee.org
Tue Dec 8 17:17:20 EST 2009
Randy Belt wrote:
> 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
>
>
I don't blame you for being confused. You're doing two things wrong
which has the side effect of making it seem to be working. The reason
it isn't obvious is that you happen to use the same ordering in your
config.file as you use initializing map_library.
Your initialization code for map_library is incorrect. You're actually
calling each of those three functions during that line, and they each do
their printing. However, this has nothing to do with the entries in
config.options. If you want to be really surprised, try printing
map_libary. It should have values of None for each of the three keys.
I suggest you move the three defs and the map_library initialization
near the beginning of your script (just after the imports). Precede it
with an import sys, and follow it with a sys.exit(0), and run just that
portion. You'll see that it prints out the three lines already. What
you actually want is not to execute each function, but to create each
function object. So change the line as follows:
map_library = {
'posmap': posmap
,'relmap': relmap
,'asnmap': asnmap
}
Note that I don't use parentheses, so the functions are not executing yet.
Now (when you remove the sys.exit()), let's look at your for-loop. For
each item in the list, you do exactly nothing. That's probably where
you want to be calling the elements of map_library. And since we moved
its initialization earlier, we can actually do that, as I'm sure you
were about to do.
DaveA
More information about the Python-list
mailing list