[Python-ideas] An ABC representing "Iterable, Sized, Container"
Guido van Rossum
guido at python.org
Thu Jul 21 17:28:55 EDT 2016
On Thu, Jul 21, 2016 at 2:14 PM, Michael Selik <michael.selik at gmail.com> wrote:
>
>
> On Thu, Jul 21, 2016 at 4:43 PM Guido van Rossum <guido at python.org> wrote:
>>
>> At Dropbox I see a lot of people who want to start using type
>> annotations (PEP 484, mypy) struggle with the collection ABCs.
>>
>> It's pretty common to want to support both sets and lists of values
>
>
> Are these folks dissatisfied with a union type: ``Union[Sequence, Set]``?
I don't know if they are, but I am. :-) Unions look pretty ugly, and
there are obvious implementations that satisfy all three criteria but
are neither sequence not set (e.g. mapping).
>> (Another useful concept is "reiterable", i.e. an Iterable that can be
>> iterated over multiple times -- there's no ABC to indicate this
>> concept. Sequence, Set and Mapping all support this concept, Iterator
>> does not include it, but Iterable is wishy-washy.)
>
>
> "Reiterable" seems to cause vocabulary confusion regularly on this list.
What kind of confusion?
> It also has a common case for runtime checking:
>
> if not isinstance(iterable, Reiterable):
> iterable = list(iterable)
>
> It's hard to think of a good name to describe ``Union[Iterable, Sized,
> Container]`` and it'd still have the confusion of whether it's re-iterable.
It's hard to imagine it having a size and not being reiterable.
FWIW IIUC the most common idiom to test for this is:
if iter(iterable) is iterable:
iterable = list(iterable)
> Does the concept of "reiterable" imply that an iteration does not mutate the
> object?
I think that's generally true, though it's not the key property. The
key property is that after iterating until the end, you can start over
from the beginning just by calling iter() again. Example types that
don't have this property include streams and anything that is itself
the result of calling iter() on something more stable.
> If so, then if something is both Reiterable and Sized, it should
> also be a Container.
Agreed. (Container is not implied by Iterable mostly because for a
once-iterable, "x in c" consumes the iterable.)
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list