Using pickle with setattr

Andrew Dalke adalke at mindspring.com
Thu Oct 14 21:04:25 EDT 2004


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