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


Groups > comp.lang.python > #41417

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

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=782592764=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; 'method.': 0.05; 'val': 0.07; 'python': 0.09; '(aka': 0.09; 'conf': 0.09; 'executes': 0.09; 'to:name:python-list': 0.15; 'eval': 0.16; 'picks': 0.16; 'subject:which': 0.16; 'variables': 0.17; 'why.': 0.17; 'module': 0.19; 'file.': 0.20; 'import': 0.21; 'skip:_ 20': 0.22; 'to:2**1': 0.23; 'header:In-Reply-To:1': 0.25; 'this?': 0.28; 'question:': 0.29; 'url:mailman': 0.29; 'this.': 0.29; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; '-----': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'skip:- 20': 0.34; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'url:org': 0.36; 'method': 0.36; 'thank': 0.36; 'two': 0.37; 'store': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'your': 0.60; 'you.': 0.61; 'received:194': 0.61; 'information': 0.63; 'person,': 0.65; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'subject:better': 0.84; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.84,865,1355094000"; d="scan'208";a="1295119"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Mon, 18 Mar 2013 14:58:44 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Laxmikant Chitare <laxmikant.general@gmail.com>, python-list <python-list@python.org>
In-Reply-To <CAFtnWG-8dndo6KYrmKvgsbiB5YkfBOgbPLrzHXauuOygvGbHtw@mail.gmail.com>
Subject Re: "eval vs operator.methodcaller" - which is better?
MIME-Version 1.0
X-Mailer Zimbra 7.2.2_GA_2852 (ZimbraWebClient - GC7 (Linux)/7.2.2_GA_2852)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3446.1363615133.2939.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363615133 news.xs4all.nl 6984 [2001:888:2000:d::a6]:40596
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41417

Show key headers only | View raw


----- Original Message -----
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Hi,

Any alternative to eval is always better than eval (aka eval is evil). 
Your code is not working in my python 2.5 env (operator has no methodcaller).

try something like

moduleName = 'mymodule'    #These two variables are read from conf file.
methodName = 'mymethod'
myModule = __import__(moduleName)
myFunc = getattr(myModule, methodName)
myFunc()


JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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


Thread

Re: "eval vs operator.methodcaller" - which is better? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-03-18 14:58 +0100

csiph-web