Why is there a difference between inspect.ismethod and inspect.isfunction from python 2 -> 3?

南楼画角 提交于 2019-11-30 11:41:59

Not specifically a difference with inspect but Python 3 in general see here

The concept of “unbound methods” has been removed from the language. When referencing a method as a class attribute, you now get a plain function object.

My suggestion for cross-platform would be:

getmembers(X, predicate=lambda x: isfunction(x) or ismethod(x))

Because essentialy there is no difference between a function and an unbound method. This idea of unbound methods exists in Python 2 mostly for historical reasons and was removed in Python 3.

This email by the BDFL himself goes into some details.


Answering your updated question. I think its best to use inspect.isroutine as it matches both unbound methods and functions with an additional benefit of also matching methods/functions implemented in C.

Start with inspect.getmembers and filter its output as you need. It also takes an optional predicate parameter.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!