within a class, redefining self with pickled file

Sean Blakey pythonista at gmail.com
Thu Apr 7 18:40:19 EDT 2005


On 7 Apr 2005 15:27:06 -0700, syd <syd.diamond at gmail.com> wrote:
> 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!
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

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:

>>> class A:
...     def change(self):
...         self = "something else entirely"
...
>>> a = A()
>>> a.change()
>>> a
<__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.
-- 
Sean Blakey
Saint of Mild Amusement, Evil Genius, Big Geek
Python/Java/C++/C(Unix/Windows/Palm/Web) developer
quine = ['print "quine =",quine,"; exec(quine[0])"'] ; exec(quine[0])



More information about the Python-list mailing list