python - python problem regarding of creation of objects using loop
问题描述
I am new to python and while I was studying a book named python GUI cookbook, I ran into a piece of code I do not fully understand:
In Line 95 to 98 the author creates a loop for making objects of radio button from Tkinter library.
In a previous lessons I learned about garbage collection. I would therefore expect in the next iteration of the loop, when the radio button is replaced, that the previous button would be reclaimed by garbage collection.
How do all three of them keep existing if they are reclaimed by garbage collection?
解决方案
这确实是一个关于 Tkinter 实现的问题。
GC 将在下一次迭代中收集对象是正确的,除非其他东西也在引用该对象。在没有阅读 Tkinter 代码库的情况下,我的假设是,当您传递win
到时Radiobutton()
,该对象会将自身添加到win
,现在保留对您的对象的引用。
此附加引用可防止 GC 收集您的对象。
开源的美妙之处在于您可以自己寻找:
最后一件事BaseWidget
是__init__
以下(self.master
是你的win
论点):
self.master.children[self._name] = self
这将类似于:
foo = []
for x in range(3):
tmp = dict(bar=x)
foo.append(tmp)
tmp
被重新分配,但基础对象的引用已添加到foo
.
推荐阅读
- python - 如何使用 rv_continuous 从高斯 kde 生成随机样本?
- sbt - 是否可以在 sbt IO.copy 命令中使用通配符
- azure - Azure Functions v3 中的 FromQuery 支持
- java - Android:onTouchEvent 中断
- python - 使用 Python 查找数字的所有除数
- php - Symfony SwiftMailer:如果控制器没有返回 $this->render() 响应,则不发送
- nestjs - 如何将 fastify 速率限制器应用于 Nest JS 中的单个路由?
- soap - 无法理解相关 XSD 文件的 SOAP 路径解析
- orm - 有没有办法在运行时动态地将属性添加到 Sails.js 中的初始化模型?
- json - Laravel - 如何从 JSON 中提取值到文本框中