【发布时间】:2017-02-07 15:48:53
【问题描述】:
我有以下示例函数:
def Run(self):
with self._resource() as r:
# a lot of code uses |r|
pass
# end of 'with' statement
# end of function body
我不想因为with 语句中的额外缩进而丢失整个函数体的视觉空间。
我也不想在类范围之外调用_resource() - 它在某些方面破坏了封装,即这不是一个好方法:
with obj._resource() as r:
obj.Run(r)
有什么漂亮的方法可以在不丢失视觉空间的情况下运行相同的代码?
【问题讨论】:
标签: python python-2.7 with-statement syntactic-sugar