How do you check whether a python method is bound or not?

后端 未结 5 1151
醉话见心
醉话见心 2020-12-15 02:54

Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it\'s bound to?

5条回答
  •  鱼传尺愫
    2020-12-15 03:27

    In python 3 the __self__ attribute is only set on bound methods. It's not set to None on plain functions (or unbound methods, which are just plain functions in python 3).

    Use something like this:

    def is_bound(m):
        return hasattr(m, '__self__')
    

提交回复
热议问题