within a class, redefining self with pickled file

S

syd

def unpickle(self):
self = pickle.load(open(self.getFilePath('pickle')))

This evidently does not work. Any idea why? I'd like to be able to
replace a lightly populated class (enough to identify the pickled
version correctly) with it's full version that's sitting pickled in a
file.

As of right now, I need to just return self and redefine the class.

Thanks!
 
S

Sean Blakey

def unpickle(self):
self = pickle.load(open(self.getFilePath('pickle')))

This evidently does not work. Any idea why? I'd like to be able to
replace a lightly populated class (enough to identify the pickled
version correctly) with it's full version that's sitting pickled in a
file.

As of right now, I need to just return self and redefine the class.

Thanks!

This problem has nothing to do with pickling. In general, assigning to
a parameter (even self) will not make a change that lasts after the
method call. For example:
.... def change(self):
.... self = "something else entirely"
....<__main__.A instance at 0x009DCD00>

Note, however, that you can MODIFY self in-place within a method. You
can probably hack together a solution that modifies self.__dict__,
self.__class__, self.__class__.__dict__, or some other magic
properties.
 
G

Greg Ewing

Note, however, that you can MODIFY self in-place within a method. You
can probably hack together a solution that modifies self.__dict__,
self.__class__, self.__class__.__dict__, or some other magic
properties.

such as

def unpickle(self):
new_self = pickle.load(open(self.getFilePath('pickle')))
self.__class__ = new_self.__class__
self.__dict__.update(new_self.__dict__)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top