
On Fri, Jan 22, 2021 at 11:10 AM Random832 <random832@fastmail.com> wrote:
On Thu, Jan 21, 2021, at 18:48, Chris Angelico wrote:
Note that slicing is NOT easy. The proposed semantics for a reversed enumeration would make slicing extremely odd.
What proposed semantics? You were the one who posted a pure-python implementation that didn't bother to implement slicing.
Ah, my bad. This thread started out on python-dev before migrating to -ideas, and I believe the proposed semantics may have been on one of the early -dev posts. The proposal was, effectively, to be the same as enumerating, making a concrete list, and then reversing (only, without actually building the list, of course). So enumerate("abc") yields [(0,'a'),(1,'b'), (2,'c')], and iterating backwards should yield those same tuples in reverse order. But if you take the reversed enumeration, iterate over it a bit, and then slice it, the semantics will be extremely bizarre.
It's easy enough to add slicing to your Enumerated class concept, though.
class Enumerated: def __init__(self, basis, indices=None): self.basis = basis self.indices = indices if indices is not None else range(len(basis)) def __getitem__(self, idx): if isinstance(idx, slice): return Enumerated(self.basis, indices=self.indices[idx]) else: return (self.indices[idx], self.basis[self.indices[idx]]) def __len__(self): return len(self.indices)
Yeah, I don't think that'll work if you slice more than once, especially with some iteration in between. The precise semantics for mixing iteration and slicing are complex enough and variable enough that you may as well just go for a concrete list at that point. ChrisA