[Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple
Jim Jewett
jimjjewett at gmail.com
Fri Sep 14 23:02:31 CEST 2012
More information about the Python-ideas mailing list
Fri Sep 14 23:02:31 CEST 2012
- Previous message: [Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple
- Next message: [Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 9/14/12, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote: > I can see why you would expect different behaviour here, though. I tend not > to think of the functions in the operator module as convenience functions > but as *efficient* nameable functions referring to operations that are > normally invoked with a non-function syntax. Which is more convenient out > of the following: > 1) using operator > import operator > result = sorted(values, key=operator.attrgetter('name')) I would normally write that as from operator import attrgetter as attr ... # may use it several times result=sorted(values, key=attr('name')) which is about the best I could hope for, without being able to use the dot itself. > 2) using lambda > result = sorted(values, key=lambda v: v.name) And I honestly think that would be worse, even if lambda didn't have a code smell. It focuses attention on the fact that you're creating a callable, instead of on the fact that you're grabbing the name attribute. > In general it is bad to conflate scalar/sequence semantics so that a caller > should get a different type of object depending on the length of a > sequence. Yeah, but that can't really be solved well in python, except maybe by never extending an API to handle sequences. I would personally not consider that an improvement. Part of the problem is that the cleanest way to take a variable number of arguments is to turn them into a sequence under the covers (*args), even if they weren't passed that way. -jJ
- Previous message: [Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple
- Next message: [Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list