On Tue, Feb 18, 2020 at 7:25 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
* The keys() method is not called in the dict constructor. Just the existence of the keys attribute is checked, its value is not used.
Given that that's already been the case, my preferred colour for the bike shed is...
1. What special method should be added, `__keys__` or `__items__`? The former returns keys, it needs to call `__getitem__` repeatedly to get values. The latter returns key-value pairs, it does not need to call `__getitem__`, but you should always pay the cost of creating a tuple even if you do not need values.
... __items__, because it'd be more efficient for the dict constructor; and if anything needs the opposite behaviour, it can check for the presence of __items__ and then simply iterate over the object, to get the keys. (Document that calling __items__ should return tuples in the same order as iteration returns keys, just like a dict does.)
2. What type should they return?
* An iterator. * An iterable which can be iterated only once. Easy to implement in Python as generator methods. * An iterable which can be iterated multiple times. * More complex view object which may support other protocols (for example support `__or__` and `__and__`).
I'd be inclined to mandate as little as possible; to be precise: it must return an iterable, but it's okay if that iterable be single-use, and it's okay either way whether it's a snapshot or a view. So any of the above would be compliant. +1 for (eventually) removing the special-case of using keys() as a signal. ChrisA