I’m new to programming and I can’t understand how garbage collection works. In the following example:
import Tkinter as tk
a = 0
class Object1:
def __init__(self):
global a
a = tk.Frame()
a.pack()
b = tk.Button(a, text="click me", command=self.callback)
b.pack()
def callback(self):
print "clicked!"
program = Object2()
class Object2:
def __init__(self):
global a
a.destroy()
a2 = tk.Frame()
a2.pack()
b = tk.Label(a2, text='This is the second object.')
b.pack()
program = Object1()
tk.mainloop()
Does ‘program,’ the instance of Object1, remain at the end? Do I have to explicitly delete it somehow? If I repeat this structure many times, with each Objectx having a button that destroys the previous frame and adds a new frame with new content, will that consume more and more memory as the program proceeds? Thanks for any help.
awill reference a “destroyed”tk.FrameTo allow the
tk.Frameto be garbage collected you need to remove that reference to itOne way is to set
a = Noneanother way would be todel aOf course when the program actually ends everything is freed