Problem with getting an option value

Bruno Desthuilliers bruno.42.desthuilliers at
Tue Apr 10 17:24:10 CEST 2007

Lucas Malor a écrit :
> Peter Otten wrote:
>> Lucas Malor wrote:
>>> The problem is options is an instance, so options."delete", for
>>> example, is wrong; I should pass options.delete . How can I do?
>> Use getattr():
> Thank you. Do you know also if I can do a similar operation with
> functions? I want to select with a string a certain get() function of
> ConfigParser:
> if   type == "int" : funcname = "getint" elif type == "bool" : 
> funcname = "getboolean" etc.

You should use a dict to do the dispatch:
   funcs = {'int':getint, 'bool', getboolean, ...}

Then you just have to:
   result = funcs[type](args...)

> How can I invoke the funcion with its name in a string?

If you used the full import, ie:
   import ConfigParser

you can use getattr() on the ConfigParser module object (yes, modules 
are objects).

If you directly imported the functions in your own module/script 
namespace, using either
   from ConfigParser import some_func
   import ConfigParser.some_func as some_func

then there's the globals() function that returns a dict of all 
names=>objects defined in the namespace, so you can use:



More information about the Python-list mailing list