[Twisted-Python] Adapter for a class, not for an instance

Andrew Bennetts andrew-twisted at puzzling.org
Wed Aug 22 02:28:11 EDT 2007


This is really a Zope question, not Twisted question.  You'll probably get
better answers by asking on a Zope mailing list.  Anyway...

Vincent Bernat wrote:
[...]
> 
> Here is what I do.
> 
> from zope.interface import Interface, implements
> from twisted.python.components import registerAdapter, Adapter
> 
> class DummyAdapter(Adapter):
>  pass
> 
> class IDummy1(Interface):
>  pass
> 
> class IDummy2(Interface):
>  pass
> 
> class Dummy1(object):
>  implements(IDummy1)
> 
> registerAdapter(DummyAdapter, IDummy1, IDummy2)
> 
> IDummy2(Dummy1()) # Works
> IDummy2(Dummy1) # Don't works
> 
> If I use registerAdapter(DummyAdapter, type, IDummy2), this works, but I
> lose a lot  of functionality (I may want to  adapt other interfaces than
> IDummy1).

Read the zope.interface documentation, particularly the distinction between
providing an interface and implmenting an interface.  If you want
IDummy2(Dummy1) to work, you need to declare Dummy1 as:

class Dummy1(object):
    classProvides(IDummy1)

However you almost certainly do not want that class to also implement(IDummy1).
It would be very unusual for a type and its instances to both provide the same
interface.

-Andrew.





More information about the Twisted-Python mailing list