How to distinguish an instance method, a class method, a static method or a function in Python 3?

后端 未结 2 1164
不知归路
不知归路 2021-01-06 16:48

I want to distinguish between methods and functions in Python 3. Furthermore, I want to get the corresponding class if it is a method. My current solution is like this:

2条回答
  •  爱一瞬间的悲伤
    2021-01-06 17:15

    I think better way to use isfunction() method of inspect.

    Syntax:

    [inspect.getmembers(, inspect.isfunction)] # will give all the functions in that module
    

    If you want to test single method, you can do so by...

    inspect.isfunction() # return true if is a function else false
    

    There are many predicates you can use along with is function. Refer to the Python 3 documentation for inspect for a clear picture.

提交回复
热议问题