Check if a function is a method of some object

后端 未结 3 418
广开言路
广开言路 2021-02-07 13:52

How to check if a function is a method of some object?

For example:

def check_method(f):
    ...

check_method(lambda x: x + 1)           # >>>          


        
3条回答
  •  野的像风
    2021-02-07 14:36

    Use inspect.ismethod().

    The documentation states:

    Return true if the object is a bound method written in Python.

    This means that it will work as you intend for classes that you define in Python. However, for methods of built-in classes like list or classes implemented in extension modules it will return False.

提交回复
热议问题