On 2020-02-18 5:34 a.m., Chris Angelico wrote:
On Tue, Feb 18, 2020 at 7:25 PM Serhiy Storchaka firstname.lastname@example.org 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...
- 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.)
I like __items__. additionally, lists should implement it to return enumerate(self), and sets should implement it to return (v, v for v in self), and as such there should be no requirement with regards to __items__ and __iter__, and whether __iter__ returns keys or values.
we can then call it __pairs__ instead of __items__. __items__ feels a lot like "outputs from __getitem__" rather than "pairs representing valid inputs and outputs of __getitem__".
- 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 _______________________________________________ Python-ideas mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://email@example.com/message/YZFEQQ... Code of Conduct: http://python.org/psf/codeofconduct/