Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mark variables as transient so they won't be pickled?

Tags:

python

pickle

Let's say I have a class:

class Thing(object):
    cachedBar = None

    def __init__(self, foo):
        self.foo = foo

    def bar(self):
        if not self.cachedBar:
            self.cachedBar = doSomeIntenseCalculation()
        return self.cachedBar

To get bar some intense calculation, so I cache it in memory to speed things up.

However, when I pickle one of these classes I don't want cachedBar to be pickled.

Can I mark cachedBar as volatile / transient / not picklable?

like image 971
SCdF Avatar asked Jun 11 '11 01:06

SCdF


Video Answer


2 Answers

According to the Pickle documentation, you can provide a method called __getstate__(), which returns something representing the state you want to have pickled (if it isn't provided, pickle uses thing.__dict__). So, you can do something like this:

class Thing:
      def __getstate__(self):
            state = dict(self.__dict__)
            del state['cachedBar']
            return state

This doesn't have to be a dict, but if it is something else, you need to also implement __setstate__(state).

like image 177
lvc Avatar answered Sep 27 '22 08:09

lvc


Implement __getstate__ to return only what parts of an object to be pickled

like image 27
AJ. Avatar answered Sep 25 '22 08:09

AJ.