[Zope3-dev] Re: adapter registration question

Philipp von Weitershausen philipp at weitershausen.de
Wed Nov 15 09:08:06 EST 2006


Chris Withers wrote:
> 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,

(the other way around)

> why would it need to be instantiated?

It instantiates a 'str' object. The 'str' object is the adapter for 
'something'. 'myStrAdapter' is the adapter factory.

>> 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?

Then what's the point of an adapter? An adapter factory is supposed to 
take at least one argument (context) = the object that it adapts. An 
adapter that doesn't adapt anything is, per definitionem, not an adapter.

> 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...

Why not leave it? No need to bend over too hard for solving everything 
with the CA :).


-- 
http://worldcookery.com -- Professional Zope documentation and training


More information about the Zope3-dev mailing list