[Python-3000] Traits/roles instead of ABCs

Ron Adam rrr at ronadam.com
Tue May 1 03:37:12 CEST 2007

Raymond Hettinger wrote:
> [Collin Winter]
>> Put another way, a role is an assertion about a set of capabilities.
>  . . .
>> If there's interest in this, I could probably whip up a PEP before the deadline.
> +100 I'm very interested in seeing a lighter weight alternative to abc.py that:
> 1) is dynamic
> 2) doesn't require inheritance to work
> 3) doesn't require mucking with isinstance or other existing mechansims
> 4) makes a limited, useful set of assertions rather than broadly covering a whole API.
> 5) that leaves the notion of duck-typing as the rule rather than the exception
> 6) that doesn't freeze all of the key APIs in concrete
> I'm concerned that the current ABC proposal will quickly evolve from optional
> to required and create somewhat somewhat java-esque landscape where
> inheritance and full-specification are the order of the day.

+100 on Raymonds list here.

I am concerned that the effect of most of the proposals will be to encode 
data as code to a greater degree.

I generally try to do the opposite.  That is, I try to make my data and 
code be independent of each other so my data is complete, and my code is 
usable for other things.

There are times I want to pipeline (or assembly line) data and mark it now 
for later dispatching at point further down stream.  In that case being 
able to temporarily and transparently attach a bit of meta data to the 
object and have it ride along with the data until some later point would be 
useful.  Then to have some nice general purpose dispatcher to initiate the 
work at that point.

A particular use case that I'm finding occurs quite often is that of 
sorting.  Not the sorting of putting things in order, but the sorting as in 
mail sorters or dividing large groups into smaller sub groups.  And of 
course that is a form of dispatching.  So far I haven't seen anything that 
directly addresses these use cases.

> IMHO, the ABC approach is using a cannon to shoot a mosquito.  My day-to-day
> problems are much smaller are could be solved by a metadata attribute or a
> role/trait solution:
> * knowing whether a __getitem__ method implements a mapping or a sequence
> * knowing whether an object can have more that one iterator (i.e a file has one
>    but a list can have many)
> * knowing whether a sequence, file, cursor, etc is writable or just readonly.
> Raymond

More information about the Python-3000 mailing list