如何判断一个函数是否是某个对象的方法?
例如:
def check_method(f):
...
check_method(lambda x: x + 1) # >>> False
check_method(SomeClass().some_method) # >>> True
在我的“helloworld”示例中,方法中有一些特殊属性(例如“im_self”、“__self__”等)。我可以依靠它们还是有更好的方法?
最佳答案
文档指出:
Return true if the object is a bound method written in Python.
这意味着它将按照您在 Python 中定义的类的预期方式工作。但是,对于 list
等内置类的方法或在扩展模块中实现的类,它将返回 False
。
关于python - 检查函数是否是某个对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963729/