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

Vincent Bernat bernat at luffy.cx
Wed Aug 22 00:10:57 MDT 2007


OoO  En  cette nuit  nuageuse  du mercredi  22  août  2007, vers  00:31,
glyph at divmod.com disait:

>> Currently, adapters can transform an instance into another
>> instance. I would like to use adapters to transform a class into
>> an instance. Does something like that already exists ?

> More correctly, adapters can transform an object into another object,
> based on what interfaces the original object provides.  So you can use
> it with class objects, if you want, just as much as you can use it
> with instance objects.

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).

> However, you probably want to ask this on the Zope Interface mailing
> list, not Twisted.  Also, I strongly suspect that whatever problem
> you're trying to solve with this would be better solved some other
> way, but that's just a hunch :).

I have some object which is the  model for some data. I want to adapt it
to display  it in HTML for example.  In this case, I  adapt the instance
because everything is  OK. Now, I want to create an  HTML form linked to
this object properties. I cannot use an instance because the object does
not exist yet.
-- 
I WILL NOT BURP IN CLASS
I WILL NOT BURP IN CLASS
I WILL NOT BURP IN CLASS
-+- Bart Simpson on chalkboard in episode 7G04




More information about the Twisted-Python mailing list