Python相当于C#的using语句[duplicate]

时间:2022-09-02 13:53:53

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

可能重复:IronPython中C#“using”块的等价物是什么?

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?)

我正在使用一些一次性.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()

4 个解决方案

#1


4  

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.

Python 2.6引入了with语句,它提供了在离开with语句时自动清理对象。我不知道IronPython库是否支持它,但它很自然。

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

具有权威性答案的Dup问题:IronPython中C#“using”块的等价物是什么?

#2


1  

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

我想你正在寻找with声明。更多信息在这里。

#3


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.

如果我理解正确,看起来等效于with语句。如果您的类定义了上下文管理器,那么它们将在with块之后自动调用。

#4


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 :

对于StreamWrite,您可以使用with statment(如果您的对象为__enter__和_exit__方法),那么您的代码将如下所示:

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

和他的__exit__方法中的StreamWriter有

sw.Dispose()

#1


4  

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.

Python 2.6引入了with语句,它提供了在离开with语句时自动清理对象。我不知道IronPython库是否支持它,但它很自然。

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

具有权威性答案的Dup问题:IronPython中C#“using”块的等价物是什么?

#2


1  

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

我想你正在寻找with声明。更多信息在这里。

#3


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.

如果我理解正确,看起来等效于with语句。如果您的类定义了上下文管理器,那么它们将在with块之后自动调用。

#4


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 :

对于StreamWrite,您可以使用with statment(如果您的对象为__enter__和_exit__方法),那么您的代码将如下所示:

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

和他的__exit__方法中的StreamWriter有

sw.Dispose()