[Zope3-dev] ContextMethod / Wrapper questions...

Shane Hathaway shane@zope.com
Fri, 27 Dec 2002 13:50:41 -0500 (EST)


On Fri, 27 Dec 2002, Steve Alexander wrote:

> Guido van Rossum wrote:
> > In fact, I'm finding that most objects that have *any* context methods
> > would be happier to have all methods be context methods.
> > 
> > 
> > Fine with me.  Would it mean we'd drop the 'wrapped_self' convention?
> > You'll have to argue that one with SteveA. :-)  <ducks>
> 
> Jim and I had a chat about that the other day.
> 
> I still quite like seeing wrapped_self (or wrapper) in place of 'self' 
> in ContextMethods. However, most people I've spoken to dislike it by 
> varying amounts. Including Jim.

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.

I'm not saying we should do this, but at least it helped me see how
context wrapping could be made less magical and how it could be integrated
into other languages, even those with static types.  I now see writing 
context-aware classes as a shortcut for writing two classes.

Shane