views:

44

answers:

1

Hi, 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?

A: 

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).

Skilldrick
It might be collected. It might not. You should never assume that you know anything about how the GC works - if you're not running out of memory, the null GC is a perfectly valid GC. If you want objects to be released, in *any* managed language, you need to do so yourself. Garbage collection is only useful in situations where you actually don't care if the resource is released, only that the underlying resource pool has sufficient capacity the next time you want it (which is usually the case with memory, but not so much with database connections, UI windows, etc.)
Nick Bastin