within a class, redefining self with pickled file

Discussion in 'Python' started by syd, Apr 7, 2005.

  1. syd

    syd Guest

    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!
     
    syd, Apr 7, 2005
    #1
    1. Advertising

  2. syd

    Sean Blakey Guest

    On 7 Apr 2005 15:27:06 -0700, syd <> 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])
     
    Sean Blakey, Apr 7, 2005
    #2
    1. Advertising

  3. syd

    Greg Ewing Guest

    Sean Blakey wrote:
    > On 7 Apr 2005 15:27:06 -0700, syd <> wrote:
    >
    >> def unpickle(self):
    >> self = pickle.load(open(self.getFilePath('pickle')))


    > 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__)

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
     
    Greg Ewing, Apr 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    604
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    610
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    417
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    389
    falcon
    Jul 31, 2005
  5. Replies:
    6
    Views:
    120
    Vivek
    Jan 25, 2006
Loading...

Share This Page