On 2020-02-18 5:34 a.m., Chris Angelico wrote:
On Tue, Feb 18, 2020 at 7:25 PM Serhiy Storchaka <storch...@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.)
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__".
> 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 _______________________________________________ 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/YZFEQQPSTPWXRD3Z6CZX6WTPKBMTV4MD/ Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ 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/EJQH42G5EOWGXDW3OCQKCQCOQ6KYEX4R/ Code of Conduct: http://python.org/psf/codeofconduct/