[Zope-dev] adapting to None

Marius Gedminas marius at gedmin.as
Sat Dec 13 06:35:07 EST 2008


On Sat, Dec 13, 2008 at 10:42:09AM +0000, Chris Withers wrote:
> Dieter Maurer wrote:
> > Then, use something different from adaptation (as adaptation does
> > not fit your wishes).
> 
> This is what I'm trying to do with subclassing, and my question was why 
> that subclassing wasn't working...

Subclassing is not always the one true solution for all problems.

> > I expect that your adapter factory can raise "ComponentLookupError"
> > when it cannot handle the adaptation and then the "default" argument
> > of the adapter lookup will take effect.
> > 
> >    adapter = IMyInterface(obj, None)
> 
> ...which might work, except I'd want that None to be the default and 
> with no need to specify it.
> 
> The pythonic way to do this would be to make IMyInterface a subclass of 
> Interface (which it is) and tweak the implementation of whatever 
> implements IMyInterface(...), which is exactly what I'm trying to do.

I don't think this is the right solution.  It makes one interface very
different from all the other interfaces.  It is a Zope Component
Architecture invariant that if 

  adapter = ISomeInterface(something)

doesn't fail with an exception, I can assume that

  ISomeInterface.providedBy(adapter)

and I can access methods and call attributes on ``adapter``.  Now, it's
perfectly understandable that you want something that doesn't act this
way, but I'd suggest you make it not look like adaptation then.

How about defining

  def GetItOrDontBother(obj):
      return IMyInterface(obj, None)

once and using it instead?  Or, if you want to distinguish "my adapter
returned None" from "I forgot to register the adapter in my test
fixture", how about

  def my_adapter(obj):
      if something:
          ...
      else:
          return NotImplemented

  def GetItOrDontBother(obj):
      adapter = IMyInterface(obj)
      if adapter is NotImplemented:
         adapter = None
      return adapter

> *That's* what I'm looking for help with, not judgement on whether 
> adapting to None is a good idea or not ;-)

Could you please describe the real problem you're solving?  We can't
help being judgemental when you seem to be asking the equivalent of "I
want to use a glass bottle to pound a nail, how can I make sure it won't
shatter?  (Trust me, I really want to use a glass bottle here.)"

-- 
http://pov.lt/ -- Zope 3 consulting and development
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope-dev/attachments/20081213/8a61ed0a/attachment.bin 


More information about the Zope-Dev mailing list