Skip to content

Instantly share code, notes, and snippets.

@j9ac9k
Created December 13, 2021 21:36
Show Gist options
  • Save j9ac9k/147f6319b5806051bb45d09fd7ab8947 to your computer and use it in GitHub Desktop.
Save j9ac9k/147f6319b5806051bb45d09fd7ab8947 to your computer and use it in GitHub Desktop.
lambda python reference count
from pyqtgraph.Qt import QtWidgets
import weakref
import gc
class MyButton(QtWidgets.QPushButton):
def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
self.clicked.connect(lambda : self.hello())
# self.clicked.connect(lambda: QtWidgets.QApplication.instance().exit())
# self.clicked.connect(self.hello)
def hello(self):
QtWidgets.QApplication.instance().exit()
app = QtWidgets.QApplication([])
button = MyButton("Hello")
button.show()
app.exec() if hasattr(app, 'exec') else app.exec_()
ref = weakref.ref(button)
del button
print(ref())
gc.collect()
print(ref())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment