Suggestion: make sequence and map interfaces more similar
Marko Rauhamaa
marko at pacujo.net
Thu Mar 31 08:36:24 EDT 2016
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'
analogously with:
>>> field_dict = { 0: "x", 1: "y", 2: "z" }
>>> list(map(field_dict.get, selector))
['y', 'y', 'x']
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.
Marko
More information about the Python-list
mailing list