On Thu, Jan 21, 2021 at 7:19 PM Chris Angelico <ros...@gmail.com> wrote: > > On Fri, Jan 22, 2021 at 11:10 AM Random832 <random...@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.
Can you give an example of what you mean?--because I'm pretty sure that we're not talking about the same thing at all. If enumerate(some_sequence) returns a sequence view, iterating over that sequence view does not advance it—just like how DictViews are not altered by iteration. Same thing if reversed(some_sequence) returns a sequence view. So, reversed(enumerate("abc"))[2] would just be (0, 'a'). You're right that there are some minor incongruities. The only one I can think of off-hand is bool(enumerate(some_sequence)) would be false when the sequence is empty. As for candidates for sequence views, I'd like to see reversed and enumerate to start since I can remember having to cast to list many times and it would be convenient not to have to. From itertools, it would be useful for thecombinatorics operators (combinations, permutations, product, etc.) to be sequence views. In fact, there are convenience functions for looking them up by index in more-itertools as this comes up a lot (at least it did for me back when I did contests). > > ChrisA > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/VTSZZK3RKSKCEG7CFHYT4TZXLMP37P3I/ > Code of Conduct: http://python.org/psf/codeofconduct/ > > -- > > --- > You received this message because you are subscribed to a topic in the Google > Groups "python-ideas" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/python-ideas/3wGART2ECXQ/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > python-ideas+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python-ideas/CAPTjJmpboVPni3FimaTh%3DNo_GBUfTmozE4kQhXBPQCrsgmkSJg%40mail.gmail.com. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YWD7JGFSUGLMFCRJKFRQYVZYTIMW25PV/ Code of Conduct: http://python.org/psf/codeofconduct/