Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'method.': 0.05; 'val': 0.07; 'conf': 0.09; 'executes': 0.09; 'to:name:python-list': 0.15; '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; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; 'question:': 0.29; 'this.': 0.29; 'file': 0.32; 'print': 0.32; 'received:74.125.82': 0.33; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'skip:- 20': 0.34; 'received:google.com': 0.34; 'subject:?': 0.35; 'there': 0.35; 'received:74.125': 0.36; 'method': 0.36; 'two': 0.37; 'to:addr:python.org': 0.39; 'subject:better': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=CwFR+jlPEi8wt4Skw33gNnUjLC7FBjLpCbXxC8vq0x8=; b=tWHWIFHY3JkNasoldNiqpKbcnqAQOwCM2ooOsQ4PEnckL/XmARYdn45x/s4q+KYPy+ a6Bo/VJyg3lwepu/F6IHGB7X4XLPGgPNfRVlGLOj2RsDHTc/RmnxmZf2+hF6LsdGAOqC mdt+v2T2KquhFHYBgeGOILL8x8OxG3e9Mzczm0HBaKNZXGoJQCuCi+v1aiQbnwhpN1lf FNK7EO1xrGOroe2aRD1eI+nTrHZGSVwejqWL6YZxTu2gyRwOVKDX1RXNwqY6o3+MS4z/ /nFFjzNLpY9oskk5TkgfzSqNctw66l4sNhvhNWpf33vNaCJlCktOhxBh0BPsmZ+QxfgY zC/w== MIME-Version: 1.0 X-Received: by 10.194.60.195 with SMTP id j3mr24788621wjr.33.1363613415307; Mon, 18 Mar 2013 06:30:15 -0700 (PDT) Date: Mon, 18 Mar 2013 19:00:15 +0530 Subject: "eval vs operator.methodcaller" - which is better? From: Laxmikant Chitare To: python-list Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363613422 news.xs4all.nl 6883 [2001:888:2000:d::a6]:56869 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41412 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