pre-PEP: Simple Thunks

Ron radam2 at tampabay.rr.com
Wed Apr 20 11:24:21 EDT 2005


Mike Meyer wrote:
> 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

Hmmm, you are correct. :-/

I was trying to not use eval().  This could be made to work, but it will 
get messy, which is another reason not to do it.

Cheers,
Ron



More information about the Python-list mailing list