[Python-3000] ABC PEP isinstance issue Was: PEP 31XX: A Type Hierarchy for Numbers (and other algebraic entities)

Jim Jewett jimjjewett at gmail.com
Thu Apr 26 00:10:23 CEST 2007


The current ABC proposal is to use isinstance as the test; Jeffrey
Yaskin's numbers PEP highlighted the weakness there with a concrete
example.

If you need to an abstraction less powerful than an existing ABC,
you're out of luck; you can't just assert that the existing class is
already sufficient, nor can you expect everyone else to use multiple
annotations.

Short of allowing more __bases__ surgery, we need a function parallel
to isinstance, which at least makes 3rd-party registration possible.
I suspect Phillip will say that we really need to make the ABCs
generic functions... but I disagree; I'm not sure that 3rd-party
adapters should even be allowed by default, but it *should* be simple
to create an ABC that does take them.

Perhaps

def isexample(obj, ABC):
    for cls in obj.__class__.__mro__:
        result = ABC.meets(obj, cls)
        if result:
            return result
    return False

class Abstract...

    # override this with a dictionary to allow 3rd-party registration
    _good_enough = ()

    @classmethod
    def meets(cls, obj=None, objclass=None):
        if objclass is cls:   # covers isinstance
            return obj
        if objclass in cls._good_enough:   # Nothing is, by default
            return cls._good_enough.[objclass](obj)

    @classmethod
    def assert_sufficient(cls, objclass, adapter):
        cls[objclass]=adapter


On 4/25/07, Jeffrey Yasskin <jyasskin at gmail.com> wrote:

> If someone needs to split them later, they can use code like::
>     import numbers
>     class IntegralDomain(Ring): ...
>     numbers.Integral.__bases__ = (IntegralDomain,) + numbers.Integral.__bases__

This only works with old-style classes, which are going away.

>>> class Abstract1(object): pass
>>> class Abstract2(object): pass
>>> Abstract1.__bases__ = (Abstract2,) + Abstract1.__bases__

Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    Abstract1.__bases__ = (Abstract2,) + Abstract1.__bases__
TypeError: __bases__ assignment: 'Abstract2' deallocator differs from 'object'


More information about the Python-3000 mailing list