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?
1 Answers python problem regarding of creation of objects using loop
This is really a question about the implementation of Tkinter.
You are correct that the GC would collect the object on the next iteration, UNLESS something else is also referencing that object. My assumption without reading the Tkinter code base is that when you pass
Radiobutton(), the object is adding itself to
win, which now retains a reference to your object.
This additional reference prevents the GC from collecting your object.
The beauty of OpenSource is you can look for yourself:
The last thing
BaseWidget does in
__init__ is the following (
self.master is your
self.master.children[self._name] = self
This would be similar to:
foo =  for x in range(3): tmp = dict(bar=x) foo.append(tmp)
tmp gets reassigned, but the underlying object's reference has been added to