我有一个文件类,你可以从中分析数据,写入数据等。我想从任何应用程序中使用它,如:
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
我试过了:
^{pr2}$但我在运行第一个代码后
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
我想这不是ContextManager的工作方式。怎么做我想要的?在
试试这个:
上下文管理器是生成器,而不是函数。在
本打算发表评论,但事情变得太复杂了,不能就此置之不理,但我确实有答案。在
代码的修正版本可以简化为
在尝试之前,让我们使用以下命令获取打开文件句柄的计数:
^{pr2}$现在使用我们的上下文管理器
是的,文件描述符计数增加了,但是现在我们已经脱离了上下文管理器,让我们看看
这就破坏了为文件设置上下文管理器的目的(我们希望使用默认的自动关闭功能,所以重新启动解释器,然后尝试这样做。在
重新运行我们的测试
现在在上下文管理器之外
是的,看起来它工作了(文件已成功关闭),但是如果有一个不存在的路径来查看异常处理呢?在
异常块确实触发了,请注意
traceback
对象,但是由于上下文管理器没有正确生成,因此会引发一个与您之前看到的不同的异常。我不知道该推荐什么,但我推荐的是记录错误(使用记录器),然后重新读取异常。您可以考虑返回某种类型的伪对象,该对象在读取时会引发异常,或者什么也不返回,但是您需要确定什么是最适合您的情况的。在相关问题 更多 >
编程相关推荐