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

Armin Ronacher armin.ronacher at active-4.com
Tue May 27 21:32:54 CEST 2008


Hi,

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.

Regards, Armin



More information about the Python-Dev mailing list