9

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

I'm writing some IronPython using some disposable .NET objects, and wondering whether there is a nice "pythonic" way of doing this. Currently I have a bunch of finally statements (and I suppose there should be checks for None in each of them too - or will the variable not even exist if the constructor fails?)

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()
Community
  • 1
  • 1
Mark Heath
  • 48,273
  • 29
  • 137
  • 194

4 Answers4

10

Python 2.6 introduced the with statement, which provides for automatic clean up of objects when they leave the with statement. I don't know if the IronPython libraries support it, but it would be a natural fit.

Dup question with authoritative answer: What is the equivalent of the C# "using" block in IronPython?

Community
  • 1
  • 1
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • thanks, that led me to this question: http://stackoverflow.com/questions/1757296/what-is-the-equivalent-of-the-c-using-block-in-ironpython, which somehow I didn't find in my previous searches – Mark Heath Oct 28 '10 at 12:50
3

I think you are looking for the with statement. More info here.

Damian Schenkelman
  • 3,505
  • 1
  • 15
  • 19
0

If I understand correctly, it looks like the equivalent is the with statement. If your classes define context managers, they will be called automatically after the with block.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

Your code with some comments :

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: # These try is useless....
            sw = StreamWriter(isfs)
            try:
                sw.Write(data)
            finally:
                sw.Dispose()
        finally: # Because next finally statement (isfs.Dispose) will be always executed
            isfs.Dispose()
    finally:
        isf.Dispose()

For StreamWrite, you can use a with statment (if your object as __enter__ and _exit__ methods) then your code will looks like :

def Save(self):
    filename = "record.txt"
    data = "{0}:{1}".format(self.Level,self.Name)
    isf = IsolatedStorageFile.GetUserStoreForApplication()
    try:                
        isfs = IsolatedStorageFileStream(filename, FileMode.Create, isf)
        with StreamWriter(isfs) as sw:
            sw.Write(data)
    finally:
        isf.Dispose()

and StreamWriter in his __exit__ method has

sw.Dispose()
kriss
  • 23,497
  • 17
  • 97
  • 116
ohe
  • 3,461
  • 3
  • 26
  • 50