【发布时间】:2018-05-23 23:19:47
【问题描述】:
如果我有一个
class A:
def foo(self):
pass
计算结果为 True:
getattr(A, 'foo') is A.foo
但这会计算为False:
a = A()
getattr(a, 'foo') is a.foo
也一样
a.foo is a.foo
为什么?
我发现getattr(a, 'foo')和a.foo都用
<bound method A.foo of <__main__.A object at 0x7a2c4de10d50>>)
所以没有提示......
【问题讨论】:
-
这不是
getattr的事情;a.foo is not a.foo也是。 -
@user2357112 为什么不呢?
-
是的,
a.foo is a.foo是False! -
@101 该链接有帮助,但 GManNickG 的答案更好,IMO。
标签: python introspection