Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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; 'args': 0.04; 'raises': 0.07; 'val': 0.07; 'conf': 0.09; 'cc:addr:python- list': 0.10; '3.3,': 0.16; 'subject:which': 0.16; 'wrote:': 0.17; 'variables': 0.17; '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; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'question': 0.27; '2.6': 0.27; 'mind.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'smart': 0.29; 'url:mailman': 0.29; 'url:python': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'another': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'skip:m 40': 0.36; 'thank': 0.36; 'two': 0.37; 'skip:o 20': 0.38; 'url:mail': 0.40; 'you.': 0.61; '2013': 0.84; 'subject:better': 0.84 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=LBHdGLgkq3NBg4FfsC8rhduRKUs1lboZeDxX092Y1K4=; b=SPV6pXIL5FP2zJdukAi/2qw0vqSMKZS3wS2CV3metfUjIKbSJXwW51wD1TsxlWhgEX 3aiS0MR0wAaLCjW6qF7OCwHysnRTgbiZYOlMslqByUEGyU8E+gcYKSD4rNOORGNOQ6Mu ko7GVFvn/P0aMNDZojuXgc3cjYMXoricSmOhdC3wg39AYQ02nBnTjDuHQWFRy77Gd8VJ sf/TlirwhZdeEyiFpD6H2STkcZ0jSWaHy62GlIIVjAMkBSrevuRWmL4U1sfmkyEC97si fH/OkFEliueU2EpIm/+Wvi8rAE1h5zvEmGc7R8Bl2QQtKdYQGXqilkZcdd6V6ofVdyLk uwdQ== MIME-Version: 1.0 X-Received: by 10.194.77.129 with SMTP id s1mr25294288wjw.17.1363615117474; Mon, 18 Mar 2013 06:58:37 -0700 (PDT) In-Reply-To: References: Date: Mon, 18 Mar 2013 19:28:37 +0530 Subject: Re: "eval vs operator.methodcaller" - which is better? From: Laxmikant Chitare To: Chris Angelico 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363615119 news.xs4all.nl 6941 [2001:888:2000:d::a6]:40291 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41416 Aha, that was smart Chris. Thank you. But this raises another question in my mind. What is the use case for operator.methodcaller ? On 3/18/13, Chris Angelico wrote: > On Tue, Mar 19, 2013 at 12:30 AM, Laxmikant Chitare > wrote: >> 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 > > Is there any reason not to do the obvious? > > val = myModule.__getattribute__(methodName)(... args ...) > > Works in 2.6 and 3.3, at least on the trivial example I tried. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list >