[Python-3000] Abilities / Interfaces

Guido van Rossum guido at python.org
Thu Nov 23 01:32:51 CET 2006


I was trying to make it so that the factory is part of library 1 and
really returns As, which however can be turned into Bs without adding
new functionality, merely by marking them as such -- but the
assumption is that we don't have a convenient way to modify the
factory so that it marks the objects it returns as Bs.

IMO the difference between ABCs and interfaces/abilities is that
inserting a new ABC into an existing class hierarchy without modifying
its source code is considered a hack, and must be done by a helper
routine that *actually* mucks with __bases__; while (Zope-style)
interfaces use an external registry or a separate class attribute
(__implements__) that is intended to be mucked with.

So perhaps it really just boils down to mucking with __bases__ or with
__implements__. I happen to know that there are cases where Python
prevents you from modifying __bases__ in certain ways because the C
part of the implementation's safety depends on what's in __bases__.
Also modifying __bases__ is expensive, involving a walk of all
subclasses. The code that implements safely setting __bases__ is a
single long function at lines 184-322 of Objects/typeobject.c in
Python (svn head). OTOH, since __implements__ has no special meaning
to Python, setting or modifying __implements__ is a simple class
attribute assignment.

On 11/22/06, Bill Janssen <janssen at parc.com> wrote:
> > I'm concerned about the additional wrinkle where there is a library
> > that calls the factory to produce A's and then turns around and passes
> > them on to *another* library. Now the other can deal with A's but has
> > recently been upgraded so that it works much better with B's; but it
> > doesn't know that A's are fine B's already. ideally, there'd be an
> > indirection in there somewhere that would let us insert a conversion
> > from A to B; but if there isn't, it would be nice to be able to poke
> > into A a marker indicating that it also supports B.
>
> There's a dangling "it" in there which I'm not sure I have the right
> antecedent for.  Let's see if I've got this straight :-).
>
>      class B (A):
>         ...
>
>      def library_1_code():
>         ...
>         value_A = factory_which_really_produces_Bs()
>         library_2_which_works_much_better_with_Bs(value_A)
>         ...
>
> Isn't the marker already implicit?  Does it matter that the first
> library only thinks of the A interface for the value?  Or are you just
> looking for a new built-in like
>
>    def markas (i, interface_class):
>      if not isinstance(i, interface_class):
>          class X (i.__class__, interface_class):
>              pass
>          i.__class__ = X
>          interface_class.__init__(i)
>      return i
>
> ?
>
> > Somehow that looks nicer with interfaces than with ABCs.
>
> I think I've lost track of the difference.
>
> Bill
>


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


More information about the Python-3000 mailing list