[Zope3-dev] Re: adapter registration question

Chris Withers chris at simplistix.co.uk
Wed Nov 15 03:35:56 EST 2006


Philipp von Weitershausen wrote:

>> ...hence the quotes. It's a "function" in that I want to use it as an 
>> adapter that doesn't need to be instantiated by a factory before being 
>> used.
> 
> All adapters need to be instantiated. 

Why?

def myStrAdapter(something):
    return str(something)

This function adapts strings to integers, why would it need to be 
instantiated?

> No. You always register a factory which is called upon lookup. That's 
> because adapters are context-dependent and thus need to be instantiated 
> every time you look them up.

What if they're not context-dependent as in my example above?

ultimately, I'm looking to replace code like the following:

convertors = {
   int:{str:str,datetime:datetime},
   str:(int:int,datetime:datetime},
}

def convert(x,y):
    return convertors[type(x)][type(y)](x)

...if you see what I mean. Apologies for the trivial examples, but these 
are real problems...

cheers,

Chris

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk



More information about the Zope3-dev mailing list