[Tutor] associating two objects without ORM and processing a text file

Alan Gauld alan.gauld at btinternet.com
Wed Feb 13 20:55:36 CET 2013


On 13/02/13 19:14, neubyr wrote:

> I am not sure how to save an object in memory to a file
> before exiting the program. Any examples or related documentation links
> would be really helpful. I am guessing it would be using some kind of
> before teardown method, but not sure about it. Any help?

If using class methods or standalone functions just call them explicitly 
at the start and end of your program. If you want to
be sure it gets called use a try/finally

try:
    Book.loadBooks(filename)  # load the data
    # do your program stuff
finally:
    Book.saveBooks(filename)   # save the data

That ensures that even if there is an exception the data will always be 
saved.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list