dumping in destructor

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Oct 20 10:37:51 CEST 2008


On Mon, 20 Oct 2008 01:12:06 -0700, Митя wrote:

> 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)
>         fh.close()
> 
> g_register = __Register() # global instance. I do not destroy it
> manually, so destructor is called on iterpreter exit

The call to `__del__()` is not guaranteed.

> can I somehow save my data from destructor?

Reliably?  No!  Change your design, it won't work this way.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list