简体   繁体   English

Python:不再使用时将变量设置为 None 的干净方法?

[英]Python: clean way to set variable to None when not used anymore?

Using excel from Python require to set all references to workbooks & excel app to None at the end to be sure it closes (using the API win32com.client).使用 Python 中的 excel 需要在最后将对工作簿和 excel 应用程序的所有引用设置为无,以确保它关闭(使用 API win32com.client)。

At first I expected to be able to use the with keyword and include all the "clean up" code in the __exit__() function. But I still have references that needs to be cleaned up when I do it this way...起初我希望能够使用with关键字并将所有“清理”代码包含在__exit__() function 中。但是当我这样做时,我仍然有需要清理的引用......

==> Is it possible to do it in a better way? ==> 是否有可能以更好的方式做到这一点? Better = all the clean up code in packaged in the exit () function. (not using contextmanager has the same issue: Excel doesn't close if I use the "with" keyword instead of "inline" python ) Better = exit () function 中打包的所有清理代码。(不使用 contextmanager 有同样的问题: 如果我使用“with”关键字而不是“inline”Excel 不会关闭 python

example: I have to set wb & xlapp to None outside for the __exit__() function示例:对于__exit__() function,我必须将 wb & xlapp 设置为None

class start_excel(object):
    def __init__(self):
        pass

    def __enter__(self):
        self.xlapp = win32.gencache.EnsureDispatch('Excel.Application')
        self.xlapp.Visible = False
        self.xlapp.Interactive = False

        return self.xlapp

    def __exit__(self, *args):
        self.xlapp.Quit()
        self.xlapp = None
        gc.collect()




class open_workbook(object):
    def __init__(self, xlapp, file_name):
        self.file_name = file_name
        self.xlapp = xlapp
     
    def __enter__(self):
        self.chemin_fichier = str(self.file_name)
        self.wb = self.xlapp.Workbooks.Open(self.chemin_fichier)
        
        return self.wb
 
    def __exit__(self, *args):
        self.wb.Close(SaveChanges=True)
        self.wb = None
        gc.collect()



with start_excel() as xlapp:
    # excel_constant = win32.constants
    with open_workbook(xlapp, file) as wb:
        wb.RefreshAll()
        xlapp.CalculateUntilAsyncQueriesDone()
        wb = None
    
    xlapp = None

thanks谢谢

Judging by this link , you can call xlapp.Application.Quit()通过此链接判断,您可以调用xlapp.Application.Quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一种在python中编写IF语句以跳过None的干净方法? - Clean way to write IF statements in python to skip on None? Python:当请求中没有值时,如何将变量设置为“ None”或“ 0”之类的值 - Python : How to set a variable to something such as “None” or “0” when there is no value from the request 在Python中,如果函数的值不是None,是否有一种简洁的方法来返回函数的值? - In Python, is there a clean way to return the value of a function if it's not None? 尽管已设置,但 Python 类变量为 None - Python class variable is None despite being set Python - 从可能返回None的函数设置变量的安全而优雅的方法 - Python - safe & elegant way to set a variable from function that may return None Python 字典,如果值为空,则将变量设置为无 - Python Dict, If value is nothing, set variable to None Python - 分配了局部变量但从未使用过 - var=None - Python - local variable is assigned but never used - var=None 当变量在python中不再更改时,如何退出for循环? - how to exit a for loop when variable doesn't change anymore in python? Python打字机函数在输入中使用时不返回任何内容? - Python typewriter function returning none when used within input? 有没有办法在Python中设置变量方法? - Is there a way to set variable methods in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM