Suggestion: make sequence and map interfaces more similar

Chris Angelico rosuav at gmail.com
Thu Mar 31 08:48:48 EDT 2016


On Thu, Mar 31, 2016 at 11:36 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> On Thu, Mar 31, 2016 at 10:22 PM, Antoon Pardon
>> <antoon.pardon at rece.vub.ac.be> wrote:
>> Okay. I'll put a slightly different position: Prove that your proposal
>> is worth discussing by actually giving us an example that we can
>> discuss.
>
> Sorry for missing most of the arguments here, but if you are talking
> about treating lists as special cases of dicts, I have occasionally
> instinctively wanted something like this:
>
>     >>> fields = [ "x", "y", "z" ]
>     >>> selector = (1, 1, 0)
>     >>> list(map(fields.get, selector))
>     Traceback (most recent call last):
>     File "<stdin>", line 1, in <module>
>     AttributeError: 'list' object has no attribute 'get'
>
> Or course, I could:
>
>     >>> list(map(fields.__getitem__, selector))
>     ['y', 'y', 'x']
>
> but that would abuse a dunder method. So I will need to:
>
>     >>> list(map(lambda i: fields[i], selector))
>     ['y', 'y', 'x']
>
> or (most likely):
>
>     >>> new_fields = []
>     >>> for i in selector:
>     ...   new_fields.append(fields[i])
>     ...
>     >>> new_fields
>     ['y', 'y', 'x']
>
>
> This tiny problem of mine could be remedied by adding a get method to
> lists.

Or, even more likely and even more Pythonic:

>>> [fields[i] for i in selector]
['y', 'y', 'x']

As soon as you get past the easy and obvious case of an existing
function, filter and map quickly fall behind comprehensions in utility
and readability.

ChrisA



More information about the Python-list mailing list