[Zope3-dev] Re: reasonable syntax for multi-adaptation
Dominik Huber
dominik.huber at perse.ch
Thu Sep 27 03:00:40 EDT 2007
Brandon Craig Rhodes wrote:
> Tres Seaver <tseaver at palladion.com> writes:
>
>> Martijn Faassen wrote:
>>
>>> IFoo.adapt() for normal adaptation
>>> IFoo.multiadapt() for multi adaptation
>> I'd rather have 'adapt' take *args for the contexts, and keywords
>> for extras (like name).
>
> And you could make the "default=" another possible extra! In which
> case our proposals at the moment compare something like this:
>
> Brandon
> Utility? IFoo()
> Single adaptation IFoo(a)
> ... with default IFoo(a, default=y)
> Multi adaptation IFoo(multi=(a,b))
> ... with default IFoo(multi=(a,b), default=y)
> Named multi adapter IFoo(multi=(a,b), name='z')
> ... with default IFoo(multi=(a,b), name='z', default=y)
This syntax is pretty comfortable because you get code completion for
free if you are using an ide (ex. eclipse pydev).
At the moment IFoo() invokes the factory of an object (if registered).
IMO the current implementation is a miss-feature because you can't
assign any parameters. I would prefer that IFoo() does look up the
default utility and IFoo(name='z') the named one.
But this change could break existing code.
>
> Martijn/Tres
> Utility IFoo.adapt()
> Single adaptation IFoo.adapt(a)
> ... with default IFoo.adapt(a, default=y)
> Multi adaptation IFoo.adapt(a,b)
> ... with default IFoo.adapt(a,b, default=y)
> Named multi adapter IFoo.adapt(a,b, name='z')
> ... with default IFoo.adapt(a,b, name='z', default=y)
>
> Well, I have to admit, yours are a lot prettier. The word "adapt"
> takes no more characters than my "multi", and parenthesis disappear!
> And though I might quibble that "adapt()" would be better spelled
> "utility()", there is a nice symmetry about your idea.
>
> - Both patterns make it explicit to anyone reading the code when a
> default value is being passed, which should vastly reduce surprise.
+1
> - Mine feels more natural and Pythonic to people who, like PvW in his
> book, think of adaptation as being like casting.
+1
--
Dominik Huber
Perse Engineering GmbH
Alte Landstrasse 6
CH-4658 Däniken
Telefon +41 56 500 01 40
Direkt +41 56 500 01 41
E-Mail dominik.huber at perse.ch
More information about the Zope3-dev
mailing list