[Python-Dev] type categories

David Abrahams David Abrahams" <dave@boost-consulting.com
Sat, 24 Aug 2002 12:45:01 -0400


From: "Nathan Clegg" <nathan@geerbox.com>


> >>>>> "Oren" == Oren Tirosh <oren-py-d@hishome.net> writes:
>
>     Oren> I would like to be able to declare that I need an object
>     Oren> with a specific interface even if the object was written
>     Oren> long before and I don't want to modify an existing library
>     Oren> just to make it conform to my interface names.
>
> class InterfaceWrapper(ExistingClass, AbstractInterfaceClass):
>       pass
>
> I'm not saying this is a good idea :), but I believe this problem is
> already solvable in the current language.  The wrapper class should
> pass the test of isinstance for the interface class, but the existing
> class as the first parent should implement all of the calls.
>
> Note that most other languages that actually support proper interfaces
> (i.e. Java) would have similar trouble adding an interface to a prior
> existing class without modifying its definition.  Python actually
> provides a much simpler solution than others might, it seems to me.

The problem is that we want to use ExistingClass *objects* where
AbstractInterfaceClass is required.

If someone else has written a module containing:

    def some_fantastic_function(AbstractInterfaceClass: x)
        ...

And I have written a function:

    def my_func(generator)
        for x in generator:
            some_fantastic_function(x)

If there's a generator lying about which produces ExistingClass, I ought to
be able to pass it to my_func.

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave@boost-consulting.com * http://www.boost-consulting.com