On Tue, Dec 10, 2019 at 03:50:19PM -0600, Tim Peters wrote:

> For me, most of the time, it's to have an obvious, uniform way to
> spell "non-destructively pick an object from a container (set, dict,
> list, deque, heap, tuple, custom tree class, ...)".  I don't even have
> iterators in mind then, except as an implementation detail.

You can't *non-destructively* pick the first (or next, or any) element 
of an iterator. Doing so changes the state of the iterator and consumes 
the element you just retrieved.

Given a container, we have:

    assert first(container) is first(container)

but the same doesn't apply to iterators.

It sounds to me that what you actually want is an analogue to 
Mapping.get that applies to all containers and sequences and leaves 
iterators completely out of it.


    a = [2, 4, 8, 16]
    a.get(0)    # returns 2
    a.get(100)  # returns None by default


I could completely get behind this idea! The only tricky part is the 
"index" isn't well-defined for mappings and sets, or tree-like 
containers.


-- 
Steven
_______________________________________________
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/KH3PQ432JBQR4E6RZEXCHBNWXU2IDKZX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to