Serhiy Storchaka wrote:
> 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.

Between __keys__ and __items__, I find that __keys__ is specifically more unique to mapping types; whereas __items__ can be reasonably applied to any type of container. Also, I personally find that I more frequently have the need to access just all of the keys and some or none of the values, rather than all of the key and value pairs. That being said, if the consensus ends up being to use __items__ instead, I would be okay with that.

Serhiy Storchaka wrote:
> 2. What type should they return?

> * An iterator.

+0.

> * An iterable which can be iterated only once. Easy to implement in
> Python as generator methods.

-1.

> * An iterable which can be iterated multiple times.

+1.

> * More complex view object which may support other protocols (for
> example support `__or__` and `__and__`).

Strong -1.

I find that the requirement of an iterable that can be iterated multiple times would be the most reasonable option, as this would be compatible with the existing dict.keys(). A simple iterator seems a bit too generic and similar to __iter__, and requiring it to be an iterable that can be iterated over only once would be incompatible with dict.keys() (which seems rather strange).

As for requiring a view object...

Steven D'Aprano wrote:
> That means that every object wanting to make use of this protocol needs
> to create a complex set-like view object, which is neither easy nor
> obvious, and may not even be appropriate for some classes.

Mainly for the above, I'm -1 on requirement of returning a view; that seems needlessly restrictive and would significantly limit the dunder method's usefulness for any user-created mappings which don't inherit from dict. If it's intended to be used for any mapping object, it should be as (reasonably) simple to implement as possible, IMO.



On Tue, Feb 18, 2020 at 7:26 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Tue, Feb 18, 2020 at 11:47:16AM -0800, Ethan Furman wrote:

> Whatever `dict.items()` returns, both for consistency and because
> `dict.items()` can become a simple alias for `dict.__items__`.

That means that every object wanting to make use of this protocol needs
to create a complex set-like view object, which is neither easy nor
obvious, and may not even be appropriate for some classes.



--
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/YZCK3XLRLV4G65EZGXQB5PU3IBA25TXP/
Code of Conduct: http://python.org/psf/codeofconduct/