python: class override “is” behavior

前端 未结 4 1510
遇见更好的自我
遇见更好的自我 2020-11-27 22:30

I\'m writing a class which encapsulates any arbitrary object, including simple types. I want the \"is\" keyword to operate on the encapsulated value, such as this behavior:<

4条回答
  •  误落风尘
    2020-11-27 23:15

    is itself cannot be overloaded, but you may be interested in other "Reflection" magic methods which may be suitable for your use case (since you are looking at this thread):

    __instancecheck__(self, instance) Checks if an instance is an instance of the class you defined (e.g. isinstance(instance, class).

    __subclasscheck__(self, subclass) Checks if a class subclasses the class you defined (e.g. issubclass(subclass, class)).

提交回复
热议问题