Mar*_*ath 6 python ironpython using
我正在使用一些一次性.NET对象编写一些IronPython,并想知道是否有一种很好的"pythonic"方式.目前我有一堆finally语句(我想在每个语句中都应该检查None)或者如果构造函数失败,变量是否甚至不存在?)
def Save(self):
filename = "record.txt"
data = "{0}:{1}".format(self.Level,self.Name)
isf = IsolatedStorageFile.GetUserStoreForApplication()
try:
isfs = IsolatedStorageFileStream(filename, FileMode.Create, isf)
try:
sw = StreamWriter(isfs)
try:
sw.Write(data)
finally:
sw.Dispose()
finally:
isfs.Dispose()
finally:
isf.Dispose()
Run Code Online (Sandbox Code Playgroud)
Python 2.6引入了该with语句,该语句提供了在离开with语句时自动清理对象.我不知道IronPython库是否支持它,但它很自然.
具有权威性答案的Dup问题:IronPython中C#"using"块的等价物是什么?
| 归档时间: |
|
| 查看次数: |
4722 次 |
| 最近记录: |