[Python-3000] Abilities / Interfaces

Gustavo Niemeyer gustavo at niemeyer.net
Thu Nov 23 13:14:17 CET 2006


> Sure, but then there's no need for a formal framwork
> for defining interfaces and adaptation -- by making
> use of duck typing, the joint user of the two libraries
> can wrap things as needed to make it all work.

You can always do everything in another way, just like
you don't need OO to be able to develop something.

> The proponents of adaptation seem to be making a
> stronger claim, however -- that somehow you can just
> register an adaptor and have all users of IFruits
> automatically understand Apples. That's the part that
> I fail to understand.

If you have an adapter from Apple to IFruit, any user
requiring an IFruit interface for an object will be
able to obtain it from Apple instances.  The "automatically"
part is not quite true, as adaptation must be explicitly
requested.

This is true for generic functions as well, except that
instead of using an IFruit interface, you have a fruit()
generic function which handles the adaptation.  Unfortunately,
this model doesn't work with more complex hierachies.

For instance:

  >>> from zope import interface, component
  >>> class Apple(object): pass
  >>> class IFruit(Interface): pass
  >>> class ITastyFruit(IFruit): pass
  >>> def to_tasty_fruit(obj): return "I'm actually a tasty fruit"
  >>> component.provideAdapter(to_tasty_fruit, (Apple,), ITastyFruit)
  >>> IFruit(Apple())
  "I'm actually a tasty fruit"

-- 
Gustavo Niemeyer
http://niemeyer.net


More information about the Python-3000 mailing list