Pre*_*Nué 13 python with-statement as-keyword contextmanager
好像我不明白 - 蟒蛇with声明.
考虑这个课程:
class test(object):
def __enter__(self): pass
def __exit__(self, *ignored): pass
Run Code Online (Sandbox Code Playgroud)
现在,当使用它时with,就像在
with test() as michael:
print repr(michael)
Run Code Online (Sandbox Code Playgroud)
我希望有一些输出像<test instance at memore blah>.但是我没有.
这里有什么问题吗?任何建议都会有帮助.
(我使用的是Python 2.6.6.)
编辑:
感谢
ephement指向我的文档.该__enter__方法应阅读
def __enter__(self): return self
Run Code Online (Sandbox Code Playgroud)