__instancecheck__ metaclasses, how do they work: why do I get True when I tuple, why doesn't print run?

Rhodri James rhodri at kynesim.co.uk
Mon Nov 4 09:54:23 EST 2019


On 04/11/2019 14:33, Veek M wrote:
>>
>> Aha.  You're trying to fix up the metaclass after the fact, which is not
>> the right way to do it.  If you change the class definitions to:
>>
> __metaclass__ = whatever; # is python2.x syntax

But not Python3: see PEP 3115

>> then you get the prints from MyMeta.__instancecheck__().  The
>> isinstance() still returns True, though, and I don't know why.  Then
>> again, your definition of MyMeta is really weird.
> 
> weird how..?
> (I'm just trying to figure out what's going on with __instancecheck__ -
> no further higher purpose :p)
> 
> example, when we do: isinstance(x, (A, B, C))
> you expect from the docs that
> A.__instancecheck__(cls, x) is passed but x = []
> also, B and C.__instancecheck__ is not called when I return False
> 
> Also if I interchange (Bar, Foo), expecting that Foo.__instancecheck__
> will be called, it's completely bypassed

That must be what's responsible for the True result.  Logically the 
problem must be to do with your MyzMeta class then.  I have no idea what.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-list mailing list