By defining the __instancecheck__() special method of a class's metaclass, one can control when an object tests as a member of that class. What if you have a predefined class P (say, a builtin class) and you want to conditionally control whether a given instance of your class C tests as an object of type P? Can this be done?
Here is a silly example:
class C:
def __init__ (self, x, y):
self.x = x
self.y = y
Suppose I want C to test as an object of class numbers.Number, but only if x > y and y % 3 == 0. (I said it would be a silly example.)
from numbers import Number
assert isinstance(C(4, 3), Number)
assert not isinstance(C(1, 3), Number)
assert not isinstance(C(4, 2), Number)
Can this be done? How?