How to check if a function is a method of some object?
For example:
def check_method(f):
...
check_method(lambda x: x + 1) # >>> False
check_method(SomeClass().some_method) # >>> True
There are some special attributes in methods in my ‘helloworld’ example (e.g. ‘im_self’, ‘__self__’ etc). Can I rely on them or there is some nicer way?
Use
inspect.ismethod().The documentation states:
This means that it will work as you intend for classes that you define in Python. However, for methods of built-in classes like
listor classes implemented in extension modules it will returnFalse.