pre-PEP: Simple Thunks

Mike Meyer mwm at mired.org
Wed Apr 20 03:15:28 EDT 2005


Ron_Adam <radam2_ at _tampabay.rr.com> writes:
> Here's yet another way to do it, but it has some limitations as well.
>
> import pickle
> def pickle_it(filename, obj, commands):
>     try:
>         f = open(filename, 'r')    
>         obj = pickle.load(f)
>         f.close()
>     except IOError:
>         pass 	
>     for i in commands:
>         i[0](i[1])
>     f = open(filename, 'w')
>     pickle.dump(obj, f)
>     f.close()
>  
> file = 'filename'
> L = []
>
> opps = [ (L.append,'more data'),
>          (L.append,'even more data') ]
> pickle_it(file, L, opps)

Um - it doesn't look like this will work. You pass L in as the "obj"
paremeter, and then it gets changed to the results of a
pickle.load. However, you call L.append in the for loop, so the data
will be appended to L, not obj. You'll lose an list elements that were
in the pickle.

   <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list