Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'method.': 0.05; 'val': 0.07; 'be:': 0.09; 'conf': 0.09; 'executes': 0.09; 'cc:addr:python-list': 0.10; 'read.': 0.13; 'feedback.': 0.15; 'chris,': 0.16; 'expert,': 0.16; 'literals,': 0.16; 'literals.': 0.16; 'picks': 0.16; 'simpler,': 0.16; 'subject:which': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'variables:': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'certainly': 0.17; 'variables': 0.17; 'why.': 0.17; 'examples': 0.18; 'module': 0.19; 'file.': 0.20; 'import': 0.21; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; "d'aprano": 0.29; 'question:': 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'probably': 0.29; 'this.': 0.29; 'expect': 0.31; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'received:74.125.82': 0.33; 'hi,': 0.33; 'skip:- 20': 0.34; 'received:google.com': 0.34; 'faster': 0.35; 'massive': 0.35; 'subject:?': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'received:74.125': 0.36; 'url:org': 0.36; 'method': 0.36; 'anything': 0.36; 'should': 0.36; 'thank': 0.36; 'two': 0.37; 'uses': 0.37; 'easier': 0.38; 'instead': 0.39; 'called': 0.39; 'url:mail': 0.40; 'your': 0.60; 'risk': 0.66; '2013': 0.84; 'michel': 0.84; 'subject:better': 0.84; 'careful': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=47ZMNqNJdgTP3HtzzQv6Td/whnXCaSQWnlLXcWb6338=; b=OMLCU5ht7Mh6qWS5z2A7EMLP5T6FD71NKvCP8pS6rByeF/byAPOMr8xwJyezGaAIpV fm4YfF22KkcEja3EUSy1Q6yDnfnW86J0As36vta+1gobFfVWYbpG2YSywkTRtKID+TVw 6o9c9ULcC3/r3JAwFfMfl5q6BzWkWI0qglmUl0bo2mNee0Ps8XgS0hckWwX+OWMCyhuy p93mPhtMHBCZK1+qdiKK2LazRK+Lq7mt/zJuS/VGlTToVYGwbADfoEY/M7b5ME4Q8CLb Z2QrMVxrzdXC+z2T324erRFqS41Nu2ZFZnTYrc4Tip+HtraFRwKm8iq08cipSHZQZ+c8 c2gw== MIME-Version: 1.0 X-Received: by 10.180.80.35 with SMTP id o3mr17236176wix.9.1363617386348; Mon, 18 Mar 2013 07:36:26 -0700 (PDT) In-Reply-To: <51472171$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <51472171$0$6599$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 18 Mar 2013 20:06:26 +0530 Subject: Re: "eval vs operator.methodcaller" - which is better? From: Laxmikant Chitare To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363617388 news.xs4all.nl 6954 [2001:888:2000:d::a6]:51796 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41428 Thank you Chris, Michel and Steven for your feedback. Steven, yes I realised that the examples are faulty. I intended to use variables instead of string literals. I will be careful next time. On 3/18/13, Steven D'Aprano wrote: > On Mon, 18 Mar 2013 19:00:15 +0530, Laxmikant Chitare wrote: > >> 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 > > Since your example code only uses string literals, the best way to write > this would be: > > import mymodule > mymodule.mymethod() > > But I expect that your example was faulty, and you intended to use > variables: > > myModule = __import__(moduleName) > myMethod = operator.methodcaller(methodName) > val = myMethod(myModule) > > > This would be simpler, and probably faster too: > > myModule = __import__(moduleName) > val = getattr(myModule, methodName)() > > > It's certainly easier to read. > > >> --------------------------- >> >> Apporach 2: >> --------------------------- >> moduleName = 'mymodule' #These two variables are read from conf file. >> methodName = 'mymethod' >> >> val = eval('myModule.' + methodName + '()') >> print val > > This example also fails, since you don't have anything called "myModule". > > I suspect you left out a line, myModule = __import__(moduleName). > > >> --------------------------- >> >> Question: Which approach is better and why. Is there any other better >> way to do this? > > > You should avoid eval, it is a massive security risk unless you are an > expert, and even then it is still a big security risk. It's also slower > than the alternatives. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list >