How do I properly manage the Python class __del__(self) method process?
-
I created a class using a module that saves run-time data within an instance dictionary object. At the end of the process I'd like to serialize this dictionary object to a file using cpickle, which I do in the __del__(self) method. Often times however Python begins the destruction process before __del__(self) is called and deletes the dictionary object before I can serialize it. How do I address this occurrence?
-
Answer:
Turn it into a regular save method and then register an exit handler to call that method when the process intends to exit. http://docs.python.org/library/atexit.html
Kevin Matzen at Quora Visit the source
Related Q & A:
- How Do I Properly Bake A Texture In Cinema 4D?Best solution by video.stackexchange.com
- How do I plan/manage a large project?Best solution by stackoverflow.com
- How do I use WordNet in Python?Best solution by Stack Overflow
- How do I properly bind ObservableCollection to listview?Best solution by Stack Overflow
- How do I add to an array from a static method?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.