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

Guido van Rossum guido at python.org
Fri Apr 7 20:33:25 CEST 2006


On 4/7/06, Walter Dörwald <walter at livinglogic.de> wrote:
> > Using @overloaded functions I would create an explicit class variable
> > which is the @overloaded adapter rather than trying to make the
> > interface also *be* the adapter. I would define the Interface class
> > like this:
> >
> > class InterfaceMetaclass(type):
> >     # I hate anonymous metaclasses
> >     def __new__(mcl, name, bases, dict):
> >         # Give each class it's own registry
> >         dict["adapter"] = overloaded(None)  # set default_function to None
> >         return type.__new__(mcl, name, bases, dict)
> >
> > class Interface:
> >     __metaclass__ = InterfaceMetaclass
> >     # No need for adapt and register methods here
> >
> > The registration would then look like this:
> >
> > Sequence.adapter.register(list)(PythonSeqAsSequence)
> >
> > and the invocation would look like this:
> >
> > print Sequence.adapter([1,2,3]).len()
>
> This looks reasonable enough, as the adapter comes for free and it
> avoids the problem of a __call__() in the Interface or its metaclass.
>
> But if I'm implementing an adapter for a certain type, I still can't
> make the returned object an instance of Interface (or I could, but it
> would inherit an adapter, which is useless),

I'm not following. Why would an adapter ever return an instance of
Interface (which is just a token object, a reification of the concept
of an interface)?

> so I would have:
>
> class Interface:
>    def getitem(self, index): ...
>    def len(self, index): ...
>
> Interface.adapter() which I have to call to do the adaption and
>
> class PythonSeqAsSequence:
>    def getitem(self, index): ...
>    def len(self, index): ...
>
> which implements the Sequence protocol, but shouldn't subclass Sequence.
>
> Somehow this feels awkward to me.

Why would the wrapper class need to subclass the interface class?

OTOH if you really like to express implementing an interface as
inheriting from that interface, what's the problem with inheriting the
adapter?

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list