【发布时间】:2013-12-12 22:51:27
【问题描述】:
我正在阅读this question,我想到我也需要它,但在 Python 中。
所以我想知道是否有办法使用 Python 中的 with 语句来做到这一点。
基本上我想要的是 Python 中的某种 IDisposable (C#) 类比。我知道,肯定会有所不同,我认为是这样的:
class ForUseInWith(IDisposableLike):
#something here
pass
并像这样使用它:
with ForUseInWith() as disposable:
#something here
pass
目前我正在研究如何做到这一点,研究python reference 和PEP 343,如果我设法做一个好的解决方案和一个聪明的例子,我会在这里发布答案。但顺便说一句,也许你可以帮助我。
【问题讨论】:
标签: python