[Python-3000] Revised PEP 3119 (Abstract Base Classes)

Guido van Rossum guido at python.org
Wed May 16 03:34:52 CEST 2007


On 5/15/07, Collin Winter <collinw at gmail.com> wrote:
> On 5/11/07, Guido van Rossum <guido at python.org> wrote:
> > - Overloading isinstance and issubclass is now a key mechanism rather
> > than an afterthought; it is also the only change to C code required
> >
> > - Built-in (and user-defined) types can be registered as "virtual
> > subclasses" (not related to virtual base classes in C++) of the
> > standard ABCs, e.g. Sequence.register(tuple) makes issubclass(tuple,
> > Sequence) true (but Sequence won't show up in __bases__ or __mro__).
>
> (The bit about "issubclass(tuple, Sequence)" currently isn't true with
> the sandbox prototype, but let's assume that it is/will be.)

Perhaps you tried it without the patch (reference [12] from PEP 3119)
applied? It works for me:

guido at pythonic:abc$ python3.0
Python 3.0x (p3yk, May 10 2007, 17:05:42)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import abc
>>> isinstance((), abc.Sequence)
True
>>>


> Given:
>
> class MyABC(metaclass=ABCMeta):
>   def foo(self): # A concrete method
>     return 5
>
> class MyClass(MyABC): # Mark as implementing the ABC's interface
>   pass
>
> >>> a = MyClass()
> >>> isinstance(a, MyABC)
> True # Good, I can call foo()
> >>> a.foo()
> 5
>
> >>> MyABC.register(list)
> >>> isinstance([], MyABC)
> True # Good, I can call foo()
> >>> [].foo()
> Traceback (most recent call last):
> AttributeError: 'list' object has no attribute 'foo'
>
> Have I missed something? It would seem that when dealing with ABCs
> that provide concrete methods, "isinstance(x, SomeABC) == True" is
> useless.

The intention is that you shouldn't register such cases. This falls
under the consenting-adults rule.

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


More information about the Python-3000 mailing list