[Python-Dev] Iterable String Redux (aka String ABC)

Guido van Rossum guido at python.org
Tue May 27 21:42:48 CEST 2008


[+python-3000]

On Tue, May 27, 2008 at 12:32 PM, Armin Ronacher
<armin.ronacher at active-4.com> wrote:
> Strings are currently iterable and it was stated multiple times that this is a
> good idea and shouldn't change.  While I still don't think that that's a good
> idea I would like to propose a solution for the problem many people are
> experiencing by introducing an abstract base class for strings.
>
> Basically *the* problematic situation with iterable strings is something like
> a `flatten` function that flattens out every iterable object except of strings.
> Imagine it's implemented in a way similar to that::
>
>    def flatten(iterable):
>        for item in iterable:
>            try:
>                if isinstance(item, basestring):
>                    raise TypeError()
>                iterator = iter(item)
>            except TypeError:
>                yield item
>            else:
>                for i in flatten(iterator):
>                    yield i
>
> A problem comes up as soon as user defined strings (such as UserString) is
> passed to the function.  In my opinion a good solution would be a "String"
> ABC one could test against.

I'm not against this, but so far I've not been able to come up with a
good set of methods to endow the String ABC with. Another problem is
that not everybody draws the line in the same place -- how should
instances of bytes, bytearray, array.array, memoryview (buffer in 2.6)
be treated?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list