What is the equivalent of the C# “using” block in IronPython?

大兔子大兔子 提交于 2019-11-27 22:53:18

IronPython supports using IDisposable with with statement, so you can write something like this:

with ClassThatImplementsIDisposable() as something:
    pass
Reed Copsey

IronPython (as of the 2.6 release candidates) supports the with statement, which wraps an IDisposable object in a manner similar to using.

With statement. For example:

with open("/temp/abc") as f:
    lines = f.readlines()

There is the with statement: http://www.ironpythoninaction.com/magic-methods.html#context-managers-and-the-with-statement

with open(filename) as handle:
    data = handle.read()
    ...

the using block is in fact the following under the hood:

try {
  (do something unmanaged here)
}
finally {
  unmanagedObject.Dispose();
}

Hope this helps you understand the logic behind the using statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!