在尝试在运行时修补上下文管理器时,我注意到以下代码的行为与我的预期不符:
class B:
def __enter__(self):
print('normal')
def __exit__(self, *stuff):
pass
def modify(self, x):
def other(self):
print('not normal: ', x)
self.__enter__ = other.__get__(self, type(self))
def main():
b = B()
b.__enter__()
b.modify('hi')
b.__enter__()
with b:
print('in with')
b.__enter__()
if __name__ == '__main__':
main()
执行,打印:
normal
not normal: hi
normal
in with
not normal: hi
main
的第一部分,显式调用__enter__
,行为符合预期(方法 正确修改),with
- 声明似乎忽略了这一点。
经过一些搜索,我发现根据PEP 343显示了一个示例翻译,它解释了行为;也就是说,with mgr: ...
的翻译内部使用类似
type(mgr).__enter__(mgr)
而不是直接的方法调用,就像我上面做的那样。
我想知道为什么这样做了。只是为了防止像我这样的人搞乱,还是有更深层次的原因?
答案 0 :(得分:0)
语言描述中定义了一些名为special method lookup的内容。基本上,这种查找(使用type(obj).__method__(obj)
)适用于所有“魔术方法”;有两个原因:
int.__hash__
与type(int).__hash__
不同,我们通常真正想要的)