[Python-3000] Abilities / Interfaces

Gustavo Niemeyer gustavo at niemeyer.net
Wed Nov 22 23:33:56 CET 2006


> Well, this assumes automatic adaptation (which is off the table), *or*
> everything that uses IFruits must always explicitly invoke some
(...)
> You've lost me here. A less abstract example might work better.

Sorry.. I should have provided a good concrete example in
the first place.  Here it goes.

implements(Apple, IFruit)
implements(Orange, IFruit)

With adapters:

  registry = AdapterRegistry()
  registry.register(IFruit, IJuice, fruit_to_juice)
  registry.register(IFruit, IJelly, fruit_to_jelly)
  apple_juice = registry.query(apple, IJuice)
  orange_jelly = registry.query(orange, IJelly)

With generic functions:

  @generic
  juice(obj): return None
  juice.register(IFruit)(fruit_to_juice)
  @generic
  jelly(obj): return None
  jelly.register(IFruit)(fruit_to_jelly)
  apple_juice = juice(apple)
  orange_jelly = jelly(orange)

Notice the subtle difference between them.  The "target" interface
is gone with generic functions.  Think of how both would be handled
without interfaces.  It can be done for sure, but interfaces do
provide additional value.
 
-- 
Gustavo Niemeyer
http://niemeyer.net


More information about the Python-3000 mailing list