Well, yes, but mutating a dict through its views wouldn’t make sense in the first place:
not for keys, but it would at least be possible for dict_items, and even potentially for dict_values, though yes, that would be really confusing.
> So I think it might be better to leave mutation out of the original version anyway unless someone has a need to it (at which point we can use the examples to think through the best answers to the design issues).
Yeah, I'm heading that way too.
> yes, but they are only a single iterator -- if you call iter() on one you always get the same one back, and it's state is preserved.
No, that’s not true. Each call to iter() returns a completely independent iterator each time, with its own independent state that starts at the head of the view.
Sorry -- total brain blip on my part -- I tested that out before posted, but had a typo that totally invalidated the test --arrg!
I'm still a bit confused about what a dict.* view actually is -- for instance, a dict_keys object pretty much acts like a set, but it isn't a subclass of set, and it has an isdisjoint() method, but not .union or any of the other set methods. But it does have what at a glance looks like pretty complete set of dunders....
Anyway, a Sequence view is simpler, because it could probably simply be an immutable sequence -- not much need for contemplating every bit of the API.
I do see a possible objection here though. Making a small view of a large sequence would keep that sequence alive, which could be a memory issue. Which is one reason why sliced don't do that by default. And it could simply be a buyer beware issue. But the more featureful you make a view, the more likely it is that they will get used and passed around and kept alive without the programmer realizing the implications of that.
Food for thought.
Now I need to think about how to write this all up -- which is why I wasn't sure I was ready to bring this up bu now I have, so more to do!
PR's accepted on my draft!
>>> d[7] = 8
>>> next(i1)
RuntimeError: dictionary changed size during iteration
>>> i3 = iter(k)
>>> next(i3)
That's probably a feature we'd want to emulate.
> Basically, views are not like iterators at all, except in that they save time and space by being lazy.
Well, this is a vocabulary issue -- an "iterable" and "iterator" is anything that follows the protocol, so yes, they very much ARE iterables (and iterators) even though they also have some additional behavior.
Which is why it's not wrong to say that a range object is an iterator, but is IS wrong to say that it's Just and iterator ...
> Such a resettable-iterator thing (which would have some precedent in file objects, I suppose) would actually be harder to Implement, on top of being less powerful and potentially confusing. And the same is true for slices.
but the dict_keys iterator does seem to do that ...