[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Cleanup when a object dies

Benjamin

1/23/2008 3:55:00 AM

I writing writing a class to allow settings (options, preferences) to
written file in a cross platform manner. I'm unsure how to go a about
syncing the data to disk. Of course, it's horribly inefficient to
write the data every time something changes a value, however I don't
see how I can do it on deletion. I've read that __del__ methods should
be avoided. So am I just going to have to force the client of my
object to call sync when they're done?
2 Answers

Raymond Hettinger

1/23/2008 5:30:00 AM

0

On Jan 22, 7:54 pm, Benjamin <musiccomposit...@gmail.com> wrote:
> I writing writing a class to allow settings (options, preferences) to
> written file in a cross platform manner. I'm unsure how to go a about
> syncing the data to disk. Of course, it's horribly inefficient to
> write the data every time something changes a value, however I don't
> see how I can do it on deletion. I've read that __del__ methods should
> be avoided. So am I just going to have to force the client of my
> object to call sync when they're done?

Lots of ways
1. Try the atexit module
2. Use a weakref callback
3. Embed a client callback in a try/finally.
4. Or, like you said, have the client call a sync() method -- this is
explicit and gives the client control over when data is written.

Raymond

Benjamin

1/24/2008 3:17:00 AM

0

On Jan 22, 11:29 pm, Raymond Hettinger <pyt...@rcn.com> wrote:
> On Jan 22, 7:54 pm, Benjamin <musiccomposit...@gmail.com> wrote:
>
> > I writing writing a class to allow settings (options, preferences) to
> > written file in a cross platform manner. I'm unsure how to go a about
> > syncing the data to disk. Of course, it's horribly inefficient to
> > write the data every time something changes a value, however I don't
> > see how I can do it on deletion. I've read that __del__ methods should
> > be avoided. So am I just going to have to force the client of my
> > object to call sync when they're done?
>
> Lots of ways
> 1. Try the atexit module
> 2. Use a weakref callback
So, I can keep a global list weakrefs to all the objects of my class
that have been created. Then have the callback call sync on them?
> 3. Embed a client callback in a try/finally.
> 4. Or, like you said, have the client call a sync() method -- this is
> explicit and gives the client control over when data is written.
>
> Raymond