[Zope3-dev] ContextMethod / Wrapper questions...
Steve Alexander
steve@cat-box.net
Fri, 27 Dec 2002 20:02:01 +0000
> I've been thinking about wrappers the past few months. Something occurred
> to me that clarified their role for me, and helped me understand how they
> might work in languages other than Python: "context methods" could be
> viewed as methods of the wrapper instance. Taking this further, when you
> write a class whose instances are meant to be wrapped, you're really
> writing two classes: one class that represents the data, and another that
> represents the behavior.
>
> Imagine it like this:
>
> class Contact(Unwrapped):
> __wrapper__ = ContactWrapper
> name = ''
> email = ''
>
> class ContactWrapper(Wrapper):
> def getName(self):
> return self.name.strip()
> def __str__(self):
> return '%s (%s)' % (self.getName(), self.email)
>
>
> The Wrapper base class would arrange for all methods to come from
> ConactWrapper while all data access goes to the appropriate Contact
> instance.
This is more like what I'd like to do with adapters like the one that
adapts containers to IZopeContainer, to include framework-specific
things like sending out appropriate events, and context-wrapping the
results of __getitem__.
I think that rather than be an adapter, this could be a new class of
component, a Decorator / Proxy. The rules for decoration/proxying could
be given in a separate class, with extra or overriding methods, as in
your example above, or as declarative directives in zcml saying which
methods should cause events to be issued, etc.
The traversal process could give you an appropriate decorator/proxy when
you traverse to an object. So, on traversing to a container, you'd get a
decorator/proxy-wrapped container. This would avoid having to adapt to
IZopeContainer when you want to mutate the container.
--
Steve Alexander