dumping in destructor

Митя netimen at gmail.com
Mon Oct 20 10:12:06 CEST 2008

I have a class which I want to save it's data automatically on disc,
when it's destroyed. I have following code:

from cPickle import dump

class __Register(object):
    def __init__(self):
        self.dict = {}
    def __del__(self):
        fh = open('aaa', 'w')
        dump(self.dict, fh)

g_register = __Register() # global instance. I do not destroy it
manually, so destructor is called on iterpreter exit

But when g_register is being destroyed, dump seems to be already dead,
so I get:

Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound method __Register.__del__ of <MyWiki.Register.__Register object
at 0x835a74c>> ignored

can I somehow save my data from destructor?

More information about the Python-list mailing list