Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41416
| References | <CAFtnWG-8dndo6KYrmKvgsbiB5YkfBOgbPLrzHXauuOygvGbHtw@mail.gmail.com> <CAPTjJmpDpqKfwx056VmR8umpX5aUPZsxNTVzGmXUkW_P_-ttYQ@mail.gmail.com> |
|---|---|
| Date | 2013-03-18 19:28 +0530 |
| Subject | Re: "eval vs operator.methodcaller" - which is better? |
| From | Laxmikant Chitare <laxmikant.general@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3445.1363615119.2939.python-list@python.org> (permalink) |
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 <rosuav@gmail.com> wrote:
> On Tue, Mar 19, 2013 at 12:30 AM, Laxmikant Chitare
> <laxmikant.general@gmail.com> 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
>
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: "eval vs operator.methodcaller" - which is better? Laxmikant Chitare <laxmikant.general@gmail.com> - 2013-03-18 19:28 +0530 Re: "eval vs operator.methodcaller" - which is better? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 14:23 +0000
csiph-web