Special Method Protocols
__instancecheck__() — class creation protocol
Metaclass hook controlling isinstance(instance, cls). Return a truth value; define on the metaclass rather than as a normal instance method. Avoid recursive isinstance calls involving the same target.
Formal Syntax & Structure
python syntaxpython
def __instancecheck__(cls, instance):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| instance | object | Object being tested. |
Return Value
bool
Whether instance qualifies.
Working Example
Example Snippetpython
class Meta(type): def __instancecheck__(cls, instance): return getattr(instance,"ready",False) is Trueclass Ready(metaclass=Meta): passclass Job: ready=Trueprint(isinstance(Job(), Ready))