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

Veek M veek at dont-use-this.com
Mon Nov 4 09:33:44 EST 2019


> 
> 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

> 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


More information about the Python-list mailing list