- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以这段代码:
from inspect import *
class X(object):
def y(self):
pass
methods = getmembers(X, predicate=ismethod)
functions = getmembers(X, predicate=isfunction)
print("%r" % methods)
print("%r" % functions)
从 python2.7 产生:
[('y', <unbound method X.y>)]
[]
从 python3.3 产生:
[]
[('y', <function X.y at 0x1006ee3b0>)]
我四处搜寻,但我看不出这种行为变化有任何明显的原因。
具体来说,为什么 python 3 将我的方法视为函数?
是否有任何跨运行时的方法来获取类的方法列表?
(即在 python2.X 和 3.X 上运行时返回相同的东西)
编辑:getmembers() 不工作的例子:
from inspect import *
class X(object):
def y(self):
pass
methods = getmembers(X)
for i in methods:
if ismethod(i):
print("Method: %s" % str(i))
else:
print("Not a method: %s" % str(i))
打印:
Not a method: ('__class__', <attribute '__class__' of 'object' objects>)
Not a method: ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>)
Not a method: ('__dict__', <attribute '__dict__' of 'X' objects>)
Not a method: ('__dir__', <method '__dir__' of 'object' objects>)
Not a method: ('__doc__', None)
Not a method: ('__eq__', <slot wrapper '__eq__' of 'object' objects>)
Not a method: ('__format__', <method '__format__' of 'object' objects>)
Not a method: ('__ge__', <slot wrapper '__ge__' of 'object' objects>)
Not a method: ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>)
Not a method: ('__gt__', <slot wrapper '__gt__' of 'object' objects>)
Not a method: ('__hash__', <slot wrapper '__hash__' of 'object' objects>)
Not a method: ('__init__', <slot wrapper '__init__' of 'object' objects>)
Not a method: ('__le__', <slot wrapper '__le__' of 'object' objects>)
Not a method: ('__lt__', <slot wrapper '__lt__' of 'object' objects>)
Not a method: ('__module__', '__main__')
Not a method: ('__ne__', <slot wrapper '__ne__' of 'object' objects>)
Not a method: ('__new__', <built-in method __new__ of type object at 0x1001e0640>)
Not a method: ('__reduce__', <method '__reduce__' of 'object' objects>)
Not a method: ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>)
Not a method: ('__repr__', <slot wrapper '__repr__' of 'object' objects>)
Not a method: ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>)
Not a method: ('__sizeof__', <method '__sizeof__' of 'object' objects>)
Not a method: ('__str__', <slot wrapper '__str__' of 'object' objects>)
Not a method: ('__subclasshook__', <method '__subclasshook__' of 'object' objects>)
Not a method: ('__weakref__', <attribute '__weakref__' of 'X' objects>)
Not a method: ('y', <function X.y at 0x1006ee3b0>)
最佳答案
与 inspect
没有特别的区别,但 Python 3 通常参见 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.
我对跨平台的建议是:
getmembers(X, predicate=lambda x: isfunction(x) or ismethod(x))
关于python - 为什么 python 2 -> 3 中的 inspect.ismethod 和 inspect.isfunction 之间存在差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019949/
我正在尝试获取我类(class)中所有方法的名称。在测试 inspect 模块如何工作时,我通过 obj = MyClass.__dict__['mymethodname'] 提取了我的方法之一。 但
我不清楚 SmaCC 正则表达式中的“isMethod”支持。 这两个来源本质上说的是同一件事 https://files.pharo.org/books-pdfs/booklet-Smacc/201
所以这段代码: from inspect import * class X(object): def y(self): pass methods = getmembers(X, predi
我是一名优秀的程序员,十分优秀!