[Python-3000] Updated and simplified PEP 3141: A Type Hierarchy for Numbers
Guido van Rossum
guido at python.org
Thu May 17 19:08:11 CEST 2007
On 5/17/07, Collin Winter <collinw at gmail.com> wrote:
> ABCs can define concrete methods. These concrete methods provide
> functionality that the child classes do not themselves provide.
You seem to be misreading my intention here. ABCs serve two purposes:
they are interface specifications, and they provide "default" or
"mix-in" implementations of some of the methods they specify. The
pseudo-inheritance enabled by the register() call uses only the
specification part, and requires that the registered class implement
all the specified methods itself. In order to benefit from the
"mix-in" side of the ABC, you must subclass it directly.
> imagine that Python didn't have the readlines() method, and that I
> wanted to define one. I could create an ABC that provides a default
> concrete implementation of readlines() in terms of readline().
> class ReadlinesABC(metaclass=ABCMeta):
> def readlines(self):
> # some concrete implementation
> def readline(self):
> If I register a Python-language class as implementing this ABC,
> "isinstance(x, ReadlinesABC) == True" means that I can now call the
> readlines() method. However, if I register a C-language extension
> class as implementing this ABC, "isinstance(x, ReadlinesABC) == True"
> may or may not indicate that I can call readlines(), making the test
> of questionable value.
> You can say that I shouldn't have registered a C extension class with
> this ABC in the first place, but that's not the point.
No, it is *exactly* the point. If you want to have functionality that
is *not* provided by some class, you should use an adaptor.
> The point is
> that for consumer code "isinstance(x, ReadlinesABC) == True" is an
> unreliable test that may or may not accurately reflect the object's
> true capabilities.
> Maybe attempting to use partially-concrete ABCs in tandem with C
> classes should raise an exception. That would make this whole issue go
The register() call could easily verify that the registered class
implements the specified set of methods -- this would include methods
that are concrete for the benefit of direct subclassing.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000