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

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 Abstract1.bases = (Abstract2,) + Abstract1.bases TypeError: bases assignment: 'Abstract2' deallocator differs from 'object'



More information about the Python-3000 mailing list