[Python-3000] Abilities / Interfaces

Baptiste Carvello baptiste13 at altern.org
Thu Nov 23 00:15:18 CET 2006


Bill Janssen a écrit :
> 
> I see nothing wrong with defining "empty" ABCs to indicate abilities
> rather than interfaces.
> 
>    class WellTested (Ability):
>      """Indicates that the type has passed the QA process"""
>      pass
> 
>    class TestedNumber (Number, WellTested):
>      ...
> 
And then if people want to add some tests to check if TestedNumber really 
implements the Ability, they can do it using docstring syntax.


class WellTested (Ability):
   """Indicates that the type has passed the QA process

   >>> myinstance=get_test_object()
   >>> hasattr(myinstance, 'attribute')
   True
   >>> myinstance.method(1,2)
   3

   """

class TestedNumber (Number, WellTested):
   ...

check_ability(TestedNumber, WellTested)


The check_ability function would just call the doctest with the appropriate test 
object (here a TestedNumber instance).

pros:

- mixin class has no behavior, hence no multiple inheritance problems

- doctest syntax allows to describe complex semantic properties without 
reinventing an ad-hoc language like Zope interfaces do

- could be done already in python 2.X

cons:

- we need another mechanism for tagging instances. Perhaps a WeakKeyDictionary 
mapping the instances to their added abilities can do.

Cheers,
BC



More information about the Python-3000 mailing list