Problem with getting an option value

Nanjundi nanjundi at gmail.com
Tue Apr 10 16:46:17 EDT 2007


On Apr 10, 10:23 am, "Lucas Malor" <80xuux... at sneakemail.com> wrote:
> 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.
>
 > How can I invoke the funcion with its name in a string?

Use exec to assign to a variable.

>>> def testPrint(msg):
...    print 'Msg: %s' % msg
...
>>> sfunc = "testPrint"
>>> exec("f = %s" % sfunc)
>>> f('Hello Python')
Msg: Hello Python

& use 'f = self.%s' % name for class methods.

Hope its not a bad practice!




More information about the Python-list mailing list