My stupidity / strange inconsistency overriding class methods

andrew cooke andrew at acooke.org
Wed Apr 20 01:52:57 CEST 2011


Hi,

I've been staring at this problem, in various forms, all day.  Am I missing something obvious, or is there some strange hardwiring of isinstance?  This is with Python 3.2.

        class A(metaclass=ABCMeta):
            @classmethod
            def __instancecheck__(cls, instance): return False
        # no override
        assert isinstance(A(), A)
        assert A.__class__.__instancecheck__(A, A())
        
        class B(type):
            def foo(self): return 42
        class C(metaclass=B):
            @classmethod
            def foo(cls): return 7
        # override
        assert C().__class__.foo() == 7

It seems to me that the above two cases are inconsistent.  ABCMeta declares __instancecheck__ just like B declares foo.  Yet C can override foo, but A is unable to override the instance check.

Please help!

Thanks,
Andrew



More information about the Python-list mailing list