[Python-3000] Adaption & generic functions [was Generic functions]

Walter Dörwald walter at livinglogic.de
Sat Apr 8 16:23:46 CEST 2006


Guido van Rossum sagte:
> On 4/7/06, Walter Dörwald <walter at livinglogic.de> wrote:
>> An Interface is an abstract class that you subclass to make it a concrete implementation.
>
> That's not the view of most people who use the word interface these days. Not in Zope (see Fred Drake's post), not in Java.
>
>> If you have an adapter that adapts to the
>> FooInterface why shouldn't it return a FooInterface object?
>
> Because implementing an interface and extending a class are separate concepts.
>
> Did you forget duck typing? Something can be a sequence without
> subclassing a common base class.

True, with adaption it's only relevant whether there's a dispatch condition in the registry that is true. The return type of the
adaption call is irrelevant.
But somehow this changes duck typing: It's no longer relevant which operation you want to perform (by doing EAFP with the method
you want to call), but whether someone has registered a adapter with a dispatch condition that matches your object (which itself
might use duck typing):
@overload
def sequence(obj):
   raise TypeError("can't convert to sequence")

@sequence.when("hasattr(obj, '__getitem__') and hasattr(obj, '__len__')")
def pythonseqassequence(obj):
   class Sequence(object):
      def __init__(self, obj): self.obj = obj
      def getitem(self, index): return self.obj[index]
      def len(self): return len(self.obj)
   return pythonseqassequence(obj)

Servus,
   Walter





More information about the Python-3000 mailing list