Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41412

"eval vs operator.methodcaller" - which is better?

Date 2013-03-18 19:00 +0530
Subject "eval vs operator.methodcaller" - which is better?
From Laxmikant Chitare <laxmikant.general@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3441.1363613422.2939.python-list@python.org> (permalink)

Show all headers | View raw


Hi,

I have a program that picks module and method name from a
configuration file and executes the method. I have found two ways to
achieve this.

Apporach 1:
---------------------------
moduleName = 'mymodule'    #These two variables are read from conf file.
methodName = 'mymethod'

import operator
myModule = __import__('mymodule')
myMethod = operator.methodcaller('mymethod')
val = myMethod(myModule)
print val
---------------------------

Apporach 2:
---------------------------
moduleName = 'mymodule'    #These two variables are read from conf file.
methodName = 'mymethod'

val = eval('myModule.' + methodName + '()')
print val
---------------------------

Question: Which approach is better and why. Is there any other better
way to do this?

Regards,
Laxmikant

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

"eval vs operator.methodcaller" - which is better? Laxmikant Chitare <laxmikant.general@gmail.com> - 2013-03-18 19:00 +0530
  Re: "eval vs operator.methodcaller" - which is better? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 14:15 +0000
    Re: "eval vs operator.methodcaller" - which is better? Laxmikant Chitare <laxmikant.general@gmail.com> - 2013-03-18 20:06 +0530

csiph-web