[Python-ideas] An ABC representing "Iterable, Sized, Container"

Gregory P. Smith greg at krypto.org
Mon Jul 25 13:19:53 EDT 2016

On Thu, Jul 21, 2016 at 11:45 PM Stephan Hoyer <shoyer at gmail.com> wrote:

> On Thu, Jul 21, 2016 at 9:04 PM, Guido van Rossum <guido at python.org>
> wrote:
>> If it really must be two words, how about SizedIterable? That suggests
>> it's a subclass of Sized and Iterable, which it is. Container doesn't
>> need separate mention, it's pretty obvious that any Iterable can
>> implement __contains__, and more people know that Iterable is an ABC
>> than Container.
> In my experiments with type annotations, we recently encountered this
> exact same issue (variables for which either sets or lists should be
> valid). My initial solution was almost exactly what you propose here: we
> wrote a SizedIterable class, simply inheriting from Sized and Iterable.
> Ultimately, we didn't find this very satisfying, because we realized that
> strings are SizedIterables (they're sequences, too), and we really didn't
> want to allow accidentally passing strings that would be interpreted as
> iterables of characters. Unfortunately, I don't think there's any good way
> in Python to specify "any sized iterable that isn't a string".

Given how often the str problem comes up in this context, I'm thinking a
common special case in type annotation checkers that explicitly exclude str
from consideration for iteration.  It's behavior is rarely what the
programmer intended and more likely to be a bug.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160725/a1d133c0/attachment.html>

More information about the Python-ideas mailing list