[Python-3000] Adaptation vs. Generic Functions

Walter Dörwald walter at livinglogic.de
Thu Apr 6 00:06:19 CEST 2006


Tim Hochberg wrote:
> Guido van Rossum wrote:
> 
>> On 4/5/06, Tim Hochberg <tim.hochberg at cox.net> wrote:
>>  
>>
>>> Walter Dörwald wrote:
>>>    
>>>
>>>> What's still missing IMHO is a way for an adapter to defer to the next
>>>> adapter in the chain, i.e. something like:
>>>>      
>>>>
>> [...]
>>  
>>
>>> The concept seems good, but I find the implementation baffling, OK, I
>>> finally figured it out. It only seems to work once though -- you can't
>>> chain super calls. Why not simply raise a DeferToNextAdapter exception?
>>>    
>>>
>> I haven't used adapters in years, and I don't recall seeing this in
>> Alex's recent posts; can either of you explain slowly what the use
>> case is and how it's typically used? The code fragments shown so far
>> aren't helping. :-(
>>  
>>
> I'm hoping that Walter can give some more realistic examples since I 
> don't have real-world experience here. The basic idea is simply to let 
> an adapter give up and let the protocol try the next adapter. This could 
> happen in a generic function, for instance, if you wanted to try some 
> fast algorithm for some specific subtypes, but the algorithm might be 
> inappropriate depending on the values of the subtypes. You don't find 
> that out till your in the adapter itself.

What I had in mind was something like this for an extensible repr():

xrepr = Protocol("xrepr")

@xrepr.register(list)
def xrepr_list(obj):
    return "[%s]" % ", ".join(xrepr(x) for x in obj)

class MyList(list):
    ...

@xrepr.register(MyList)
def xrepr_MyList(obj):
    return "MyList(%s)" % xrepr.super(obj)

Servus,
    Walter

> This uses Nick Coghlan's GenericFunction implementation from a few posts 
> ago with the DeferToNextAdapter stuff added.

But raising a exception ends the execute of the adapter there. I can't 
use the result of the super adapter.

Servus,
    Walter



More information about the Python-3000 mailing list