<p>我有一个文件类,你可以从中分析数据,写入数据等。我想从任何应用程序中使用它,如:</p>
<pre><code>f = MyFileClass() # __init__ method puts a lot of default data in object
with f.open() as file: # where f.open() is custom MyFileClass method
file.write("foo") # file should close automatically after this
</code></pre>
<p>我试过了:</p>
^{pr2}$
<p>但我在运行第一个代码后</p>
<p><code>line 22, in fill_new_file
with f.open() as file:
File "C:\Python34\lib\contextlib.py", line 61, in __enter__
raise RuntimeError("generator didn't yield") from None
RuntimeError: generator didn't yield</code></p>
<p>我想这不是ContextManager的工作方式。怎么做我想要的?在</p>