草庐IT

ismethod

全部标签

python - 为什么 python 2 -> 3 中的 inspect.ismethod 和 inspect.isfunction 之间存在差异?

所以这段代码:frominspectimport*classX(object):defy(self):passmethods=getmembers(X,predicate=ismethod)functions=getmembers(X,predicate=isfunction)print("%r"%methods)print("%r"%functions)从python2.7产生:[('y',)][]从python3.3产生:[][('y',)]我四处搜寻,但我看不出这种行为变化有任何明显的原因。具体来说,为什么python3将我的方法视为函数?是否有任何跨运行时的方法来获取类的方法列

python - 为什么 python 2 -> 3 中的 inspect.ismethod 和 inspect.isfunction 之间存在差异?

所以这段代码:frominspectimport*classX(object):defy(self):passmethods=getmembers(X,predicate=ismethod)functions=getmembers(X,predicate=isfunction)print("%r"%methods)print("%r"%functions)从python2.7产生:[('y',)][]从python3.3产生:[][('y',)]我四处搜寻,但我看不出这种行为变化有任何明显的原因。具体来说,为什么python3将我的方法视为函数?是否有任何跨运行时的方法来获取类的方法列

Python:inspect.ismethod 如何工作?

我正在尝试获取我类(class)中所有方法的名称。在测试inspect模块如何工作时,我通过obj=MyClass.__dict__['mymethodname']提取了我的方法之一。但现在inspect.ismethod(obj)返回False而inspect.isfunction(obj)返回True,我不明白为什么。是否有一些奇怪的方法将方法标记为我不知道的方法?我认为这只是它在类中定义并将self作为其第一个参数。 最佳答案 您正在看到Python幕后机制的一些效果。当您编写f=MyClass.__dict__['mymet