Suggestion: make sequence and map interfaces more similar

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Thu Mar 31 08:55:37 EDT 2016


Marko Rauhamaa writes:

> Chris Angelico 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'

operator.itemgetter(*selector)(fields) # ==> ('y', 'y', 'x')

> analogously with:
>
>     >>> field_dict = { 0: "x", 1: "y", 2: "z" }
>     >>> list(map(field_dict.get, selector))
>     ['y', 'y', 'x']

operator.itemgetter(*selector)(field_dict) # ==> ('y', 'y', 'x')

It's not quite the same but it's close and it works the same for
strings, lists, dicts, ...



More information about the Python-list mailing list