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

Laxmikant Chitare laxmikant.general at gmail.com
Mon Mar 18 09:58:37 EDT 2013


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 at gmail.com> wrote:
> On Tue, Mar 19, 2013 at 12:30 AM, Laxmikant Chitare
> <laxmikant.general at 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
>



More information about the Python-list mailing list