[Python-3000] Composable abstract base class? (original) (raw)

Guido van Rossum guido at python.org
Sun May 27 14:57:15 CEST 2007


On 5/27/07, Nick Coghlan <ncoghlan at gmail.com> wrote:

Guido van Rossum wrote: > Ryan is repeating the classic flatten example: strings are iterables > but shouldn't be iterated over in this example. This is more the > domain of Generic Functions, PEP 3124. Anyway, the beauty of PEP 3119 > is that even if PEP 3124 were somehow rejected, you could add > Composable yourself, and there is no requirement to add it (or any > other category you might want to define) to the "standard" set of > ABCs.

I think this is an interesting example to flesh out though - how would I express that most instances of Iterable should be iterated over when being Flattened, but that certain instances of Iterable (i.e. strings) should be ignored? For example, it would be nice to be able to write: from abc import Iterable class Flattenable(Iterable): pass Flattenable.deregister(basestring)

Reading the PEP as it stands, I believe carving out exceptions like this would require either subclassing ABCMeta to change the behaviour, or else relying on PEP 3124 or some other generic function mechanism.

You can't do it with the existing ABC class, but you could do it by overriding subclasscheck in a different way. But it's definitely much easier to do with GFs.

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



More information about the Python-3000 mailing list