What's the equivalent of this in IronPython? Is it just a try-finally block?
using (var something = new ClassThatImplementsIDisposable())
{
// stuff happens here
}
What's the equivalent of this in IronPython? Is it just a try-finally block?
using (var something = new ClassThatImplementsIDisposable())
{
// stuff happens here
}
IronPython (as of the 2.6 release candidates) supports the with statement, which wraps an IDisposable object in a manner similar to using.
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.
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()
...
Having used C#, Python but not IronPython:
This usage of "using" is syntactic sugar, nothing more.
With statement. For example:
with open("/temp/abc") as f:
lines = f.readlines()
IronPython supports using IDisposable
with with
statement, so you can write something like this:
with ClassThatImplementsIDisposable() as something:
pass