Using pickle with setattr

dataangel k04jg02 at kzoo.edu
Fri Oct 15 03:48:42 CEST 2004


Sorry I wasn't clear, I didn't even realize you could simply assign 
stuff to instance variables without them being formerly declared, I'm 
still in the C mindset :P Taking this into account, I asked if my 
question was stupid ;)

I didn't test this myself because there are alot of different types of 
things I could add to a class instance, list, file objects, etc. and it 
wasn't clear what would work from my limited ability to interpret the 
pickle.py. I'll take a better look at the docs next time around.

Thanks for the help,

dataangel

Andrew Dalke wrote:

> dataangel wrote:
>
>>> I'm wondering whether or not the standard Pickle module will handle 
>>> this or not -- will an object pickle correctly if it has methods 
>>> that are not in its original class definition?
>>
>
> Instances pickle references to class by name, not by value.
> If you have restore to a class with the same name but different
> semantics then it may or may not work.  It depends on how it
> was coded.
>
>>> That I'm not planning to do -- I'm changing the attributes of the 
>>> object itself.
>>
>
> Changing the attributes of the instance or the class?  The former
> goes into the pickle'd dictionary, the latter does not.
>
>>> If it won't, what are my options? Also, is there a module available 
>>> for saving the complete state of an object such that it is loadable 
>>> without the python source for the original class?
>>
>
> No.  Consider
>
> import os
>
> class Spam(object):
>   my_os = os
>
> How can pickle save all of 'os'?  Or consider
>
> class Read:
>   infile = open("/etc/passwd")
>   def __init__(self):
>     self.line = self.infile.readline()
>
> File objects can't be pickled.
>
>
>> I'm flabbergasted. Nobody on the python mailing list knows? I've 
>> stumped the experts? ;)
>>
>> Or maybe my question is stupid for somer reason? :P
>
>
> You weren't clear about what you wanted.  It's hard for us
> to read your mind.  You could have gotten some of these answers
> by reading the docs at and around
>   http://www.python.org/doc/lib/node65.html
>
> and through experimentation.
>                 Andrew
>                 dalke at dalkescientific.com






More information about the Python-list mailing list