python的`with`语句目标是意外的无

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)

eph*_*ent 18

with文档:

如果目标包含在with语句中,__enter__()则会为其分配返回值.

如果您def __enter__(self): return self,则生成您的预期输出.