Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41416 > unrolled thread
| Started by | Laxmikant Chitare <laxmikant.general@gmail.com> |
|---|---|
| First post | 2013-03-18 19:28 +0530 |
| Last post | 2013-03-18 14:23 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Laxmikant Chitare <laxmikant.general@gmail.com> |
|---|---|
| Date | 2013-03-18 19:28 +0530 |
| Subject | Re: "eval vs operator.methodcaller" - which is better? |
| Message-ID | <mailman.3445.1363615119.2939.python-list@python.org> |
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
>
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-18 14:23 +0000 |
| Message-ID | <5147235f$0$6599$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41416 |
On Mon, 18 Mar 2013 19:28:37 +0530, Laxmikant Chitare wrote:
> Aha, that was smart Chris. Thank you.
>
> But this raises another question in my mind. What is the use case for
> operator.methodcaller ?
The use-case is mostly to allow people to write code in a functional
style, if they so choose.
import operator
func = operator.methodcaller("spam")
items = map(func, [a, b, c, d])
is the functional-style equivalent of:
items = [obj.spam() for obj in [a, b, c, d]]
methodcaller makes a little more sense if you provide arguments:
func = operator.methodcaller("spam", 1, 2, None, "ham")
items = map(func, [a, b, c, d])
compared to:
items = [obj.spam(1, 2, None, "ham") for obj in [a, b, c, d]]
I expect that the methodcaller version will be very slightly faster in
this case, since it doesn't have to parse the arguments every time the
method is called.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web