[Python-3000] Adaptation vs. Generic Functions

Walter Dörwald walter at livinglogic.de
Wed Apr 5 22:16:32 CEST 2006


Guido van Rossum wrote:

> Fascinating ideas in this thread!
> 
> I spent some time blogging about this on artima:
> http://www.artima.com/weblogs/viewpost.jsp?thread=155123
> 
> I have to write my slides for a talk about Py3K later today, but I'll
> be back. In the mean time I've rejected PEPs 245 and 246 in
> expectation of something better that's imminent!

What's still missing IMHO is a way for an adapter to defer to the next 
adapter in the chain, i.e. something like:

class Protocol(object):
    def register(self, ...):
       ...

    def __call__(self, ...):
       ...

    def default(self, *args, **kwargs):
       raise TypeError

    def candidates(self, *args, **kwargs):
       return type(args[0]).__mro__

    def super(self, *args, **kwargs):
       search = iter(self.candidates(*args, **kwargs))
       for key in search:
          if key in self.registry:
             break
       try:
          return search.next()(*args, **kwargs)
       except StopIteration:
          return self.default(*args, **kwargs)


With this an adapter could use the super() method to call the next 
adapter in the chain:

p = Protocol()

@p.register(Foo)
def p_foo(foo):
    p.super(foo)

Servus,
    Walter


More information about the Python-3000 mailing list