[Twisted-Python] Adapter for a class, not for an instance
Hi ! 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 ? Thanks.
On 01:48 pm, bernat@luffy.cx wrote:
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. 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 :).
OoO En cette nuit nuageuse du mercredi 22 août 2007, vers 00:31, glyph@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
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.
participants (3)
-
Andrew Bennetts
-
glyph@divmod.com
-
Vincent Bernat