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

Chris Barker - NOAA Federal chris.barker at noaa.gov
Fri Jul 29 15:18:11 EDT 2016


> For cases where str instances are handled differently from other
> iterables, but still within the same function, a convention of writing
> "Union[str, Iterable]" as the input parameter type may suffice -

The problem here is that often folks want to NOT allow single strings,
but rather want only an iterable of strings. But, of course, a single
string is an iterable of strings...

I'm pretty sure this is by far the most common dynamic typing gotcha
in Python ( at least after true division). Which means it may well be
worth special casing all over the place to make it easy to do right.
Personally, I'd like a solution that helps folks write correct code
even without a type checker.

AFAIK, the "solution" at this point is for every function that takes
an iterable of strings to have code like:

in = [in] if type(in) is str else in

But this is ugly and fragile in so many ways...

Perhaps there should be (maybe only in my private lib) a
"as_sequence_of_strings()" function that does this, but more
completely and robustly. Similar to numpy's asarray(), which passes
back the object of it's already an array of the type specified, and
tries to convert it otherwise.

>  typically want to consider types like str, bytes,
> bytearray and memoryview as atomic objects, rather than as containers,

There are all kinds of things that one might want to treat as atomic,
rather than a sequence -- but what is special about str is that it is
infinitely recurs able---no matter how many times you index it, you
always get back a sequence -- never an atomic object.

Most (every?) other sequence returns an atomic object (eventually)
when you index it.

So maybe the solution really is to have a character type -- then a
string is a sequence of characters, rather that a sequence of string,
and all these problems go away.

If a character object has all the methods of a string except indexing
(and iterating) it might even be mostly backward compatible....

Otherwise, special casing in multiple cases may be the only option.

Or maybe all we need is a way to spell "any iterable of strings except
a string", though that would only help type checking.

But either way, it would be great to support this better.

-CHB


More information about the Python-ideas mailing list