Type hinting for any object

后端 未结 9 1639
梦毁少年i
梦毁少年i 2020-12-14 05:43

I\'ve been working on code that\'s intended to be used with objects, without really caring what the kind of object is. I wanted to type hint that the method being written ex

9条回答
  •  执笔经年
    2020-12-14 06:26

    There is no built-in mechanism to do this without requiring all users of your interface to extend a specified class. But why would you want to do this anyway? What do all object types have in common that's enough to make them suitable input for your API?

    In all probability you wouldn't gain anything even if able to type hint like this. On the other hand, type hinting a parameter to implement an interface (such as Traversable) would be much more meaningful.

    If you still want something akin to type hinting, the best you can do is substitute a runtime check with is_object on the parameter.

提交回复
热议问题