我使用atexit
注册的方法在程序退出之前禁用程序正在控制的硬件系统。但是在执行程序时崩溃,错误消息是它找不到该程序以前使用的变量。Python是否在atexit方法运行之前销毁变量?
程序看起来像这样(无法运行!)
import atexit
class MainWindow(QtWidgets.QMainWindow, ui_mainwindow.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.testVariable
atexit.register(self.cleanup)
def cleanup(self)
self.testVariable = something #when the program reaches this point it crashes
def loop(self) #this runs connected to a QTimer
self.testVariable = something
错误消息是'MainWindow' object has no attribute 'testVariable'
假设您的代码实际上(正确地)被格式化为以下格式,我已指出错误发生的位置。
class MainWindow(QtWidgets.QMainWindow, ui_mainwindow.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.testVariable # <--- this will fail, you're doing a getattr on a non-existent variable
atexit.register(self.cleanup)