Search code examples
pythonoopobjectdestruction

how python manage object delete or destruction


guys, I am rather new to python and learning it to build a gui application (with wypython). I have a question related with object destruction in python. e.g. in myFrame I have onNew (create a new document) and onOpen (open a file) method.

briefly, it looks like this.

def onNew
    self.data=DataModel()
    self.viewwindow=ViewWindow(self.data)


def onOpen
    dlg = wx.FileDialog(self, "Open file", os.getcwd(), "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.data=DataModel.from_file(...)
            self.view=View(self.data)

now, I want to consider "if the user click open or new again, after he click either before."

so for the window classes, I could call the self.viewwindow.Destroy() to destry the windows. what about the data model object? If I first call new: self.data=DataModel(), then call open and re-assign self.data=DataModel.from_file(...), what about the old instance? Do I need destruct it myself or python will manage this destruction?


Solution

  • Python has garbage collection. As long as you don't have any references to the old object hanging around it will be collected.

    As soon as you say self.data = somethingElse then the old self.data won't have any references to it (unless another object had a reference to your object's self.data).