My stupidity / strange inconsistency overriding class methods

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


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):
            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):
            def foo(cls): return 7
        # override
        assert C() == 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!


More information about the Python-list mailing list