
On Fri, Jul 31, 2020 at 2:56 PM Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
On Fri, 31 Jul 2020 at 22:14, Chris Angelico <rosuav@gmail.com> wrote:
So, constructing a tuple or list from the keys or items WILL give you a sequence.
Yes. Since now dicts are ordered by insertion, also keys, values and items are ordered the same way.
It seems to me more simple to add some sequence methods to dict views, like subscript, slicing and index(), instead of creating other 3 methods and 3 data types.
Maybe it is common in numpy and pandas to keep adding operations to the same object until it breaks, but the key and items views already implement the Set ABC, and I'd rather refrain from having them *also* implement the Sequence ABC. For one thing, when they want to pass one of those views as an argument to another function *as a set or as a sequence*, this would force the user to show the reader what they meant, and this will make the code more readable. It would also clarify whether some other mapping supports the same operation (the presence of the new method would indicate it).
What I have not understood well is when you need to index a dict by position or slice it.
I'm guessing that indexing by 0, if it were possible, would be a convenient idiom to implement the "first item" operation that has been requested numerous times (at least for dicts). Slicing would be useful to get the first N items of a huge dict without materializing the full list of items as a list object, which brought Chris B to request this in the first place. The dict would have to be huge for this materialization to matter, but Chris B did encounter the situation. What I don't understand yet is how *frequently* the latter operation would be useful -- if it's infrequently, Chris B's own solution using islice() on the items() view looked pretty decent to me, and not that hard to come up with for someone who made it that far. For the former I expect that sooner or later someone will write a PEP and it will be accepted (assuming the PEP doesn't overreach). -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>